

*------------------------------------------------------------------------------------
* Uganda Attitudes
* Purpose: Generate variables

* Notes: 
* 0. this file consists of five parts: (1) gen outcome variables y_it; (2) gen baseline variables y_i0; (3) match treated info data; (4) gen Anderson summary index; (5) gen variables used for heterogeneity analysis
* 1. midline, endline, phone2, and endline2 data are harmonized; outcome variables are constructed according to PAP
* 2. neutrals are resolved within surveys; pecuniary measures are winsorized within surveys
* 3. CPI deflation: baseline (dec 2019), midline (oct 2020), endline (may 2021), phone2 (aug 2021), endline2 (mar 2022)
* 4. different variables may be used to construct Anderson summary index across waves

*------------------------------------------------------------------------------------



*++++ Gen outcome variables ++++*
capture program drop MidlineEndlinePhone2Endline2
program MidlineEndlinePhone2Endline2

*==== Gen wave dummy & flag ====*
gen wave = .
	replace wave = 1 	if _merge_midline == 3 // 1119 respondents
	replace wave = 2 	if _merge_endline == 3 // | _merge_endline_remote == 4 // 1041 respondents
	replace wave = 3	if _merge_phone2 == 3 // 1071 respondents
	replace wave = 4 	if _merge_endline2 == 3 // 901 respondents

* Flag for wave
gen wave_flag = .
	replace wave_flag = 1 	if _merge_midline != .
	replace wave_flag = 2 	if _merge_endline != . | _merge_endline_remote != .
	replace wave_flag = 3	if _merge_phone2  != .
	replace wave_flag = 4 	if _merge_endline2 != . 
	
gen phone_survey = (_merge_midline == 3 | e_remote == 1 | _merge_phone2  == 3 | e2_remote == 1) 


gen survey_date = .
	replace survey_date = dofc(m_starttime) 	if wave_flag == 1
	replace survey_date = dofc(e_starttime) 	if wave_flag == 2
	replace survey_date = dofc(p2_starttime) 	if wave_flag == 3
	replace survey_date = dofc(e2_starttime) 	if wave_flag == 4

// some survey response survey date is wrong < 22100 (too early), replace with survey date mean in the same wave
qui sum survey_date if wave == 1 	// 1 too early date updated
	replace survey_date = `r(mean)' if wave == 1 & survey_date < 22100
qui sum survey_date if wave == 2 	// 9 too early date updated
	replace survey_date = `r(mean)' if wave == 2 & survey_date < 22100
qui sum survey_date if wave == 3
	replace survey_date = `r(mean)' if wave == 3 & survey_date < 22100
qui sum survey_date if wave == 4
	replace survey_date = `r(mean)' if wave == 4 & survey_date < 22100

gen control = treatment == 6

destring e_hh_mem_o5 e2_hh_mem_o5, replace 	// Number of household members (calculated only in endline and endline2)

*==== Gen 3pt Likert questions (1 = positive, 2 = no effect, 3 = negative) ====*
foreach var of varlist e_g12 e_g13 e_g14 e_g17 e_g18 e_g19 e2_g12 e2_g13 e2_g14 e2_g17 e2_g18 e2_g19 {
	gen `var'_tmp = .
	replace `var'_tmp = 1 	if `var' == 1
	replace `var'_tmp = 2 	if `var' == 3
	replace `var'_tmp = 3 	if `var' == 2	
} 

foreach var of varlist e_g15 e_g16 e_g20 e_g21 e2_g15 e2_g16 e2_g20 e2_g21 {
	gen `var'_tmp = .
	replace `var'_tmp = 1 	if `var' == 2
	replace `var'_tmp = 2 	if `var' == 3
	replace `var'_tmp = 3 	if `var' == 1	
}

*==== Gen outcome variables ====*

**** Domain 1: Support for inclusive refugee hosting // 11 outcomes （3 not included in PAP）
forval i = 1/14 {
	gen d1_`i' = .
}

* 1. m_j1/e_j1/e2_j1: Overall, during coronavirus, I am in favor of Uganda hosting and assisting refugees.
	replace d1_1 = m_j1 	if wave == 1
	replace d1_1 = e_j1 	if wave == 2
	replace d1_1 = e2_j1 	if wave == 4	
	la var d1_1 "\shortstack{Supports\\Hosting\\Current}"
	
	gen d1_1_strongagree = (d1_1 == 1) 		if d1_1 != .
	gen d1_1_strongdisagree = (d1_1 == 5) 	if d1_1 != .
	la var d1_1_strongagree "\shortstack{Strongly\\Supports\\Refugee\\Hosting$^+$}"
	la var d1_1_strongdisagree "\shortstack{Strongly\\Opposes\\Refugee\\Hosting$^+$}"

* 2. m_j6/e_j10/: After coronavirus, I am in favor of Uganda hosting and assisting refugees.
	replace d1_2 = m_j6 	if wave == 1
	replace d1_2 = e_j10 	if wave == 2
	la var d1_2 "\shortstack{Supports\\Hosting\\Post-COVID}"

* 3. m_j2/e_j6/e2_j6: In July refugees from Congo were allowed to come to Uganda. They were tested for coronavirus, quarantined, and settled into camps. I am in favor of allowing refugees who test negative to move to Uganda right now./Uganda should accept more refugees.
	replace d1_3 = m_j2 	if wave == 1
	replace d1_3 = e_j6		if wave == 2
	replace d1_3 = e2_j6	if wave == 4	
	la var d1_3 "\shortstack{More\\Refugees\\Current}"

	gen d1_3_strongagree = (d1_3 == 1) 		if d1_3 != .
	gen d1_3_strongdisagree = (d1_3 == 5) 	if d1_3 != .
	la var d1_3_strongagree "\shortstack{Strongly\\Supports\\More\\Refugees$^+$}"
	la var d1_3_strongdisagree "\shortstack{Strongly\\Opposes\\More\\Refugees$^+$}"
	
* 4. m_j8/e_j12/ : After coronavirus ends, Uganda should accept more refugees.
	replace d1_4 = m_j8 	if wave == 1
	replace d1_4 = e_j12 	if wave == 2
	la var d1_4 "\shortstack{More\\Refugees\\Post-COVID}"

* 5. m_j3/e_j2/e2_j2: During coronavirus, Uganda should relocate all refugees to live in the settlements, including those currently living in Kampala.
	replace d1_5 = m_j3 	if wave == 1
	replace d1_5 = e_j2 	if wave == 2
	replace d1_5 = e2_j2 	if wave == 4
	la var d1_5 "\shortstack{Freedom of\\Movement\\Current}"
	
	gen d1_5_strongagree = (d1_5 == 5) 		if d1_5 != . & (wave == 1 | wave == 2)
		replace d1_5_strongagree = 1 		if e2_j2 == 1
		replace d1_5_strongagree = 0 		if e2_j2 != 1 & e2_j2 != .
	gen d1_5_strongdisagree = (d1_5 == 1) 	if d1_5 != . & (wave == 1 | wave == 2)
		replace d1_5_strongdisagree = 1 	if e2_j2 == 5
		replace d1_5_strongdisagree = 0 	if e2_j2 != 5 & e2_j2 != .	
	la var d1_5_strongagree "\shortstack{Strongly\\Supports\\Freedom of\\Movement$^+$}"
	la var d1_5_strongdisagree "\shortstack{Strongly\\Opposes\\Freedom of\\Movement$^+$}"
	
* 6. /m_j4/ : For those who answered "agree" or "strongly agree": Should the relocation be permanent or only during coronavirus?
	replace d1_6 = 1 	if m_j3 != .
	replace d1_6 = 0 	if m_j4 == 1 // = 1 if disagree; = 0 if agree
	la var d1_6 "\shortstack{Freedom of\\Movement\\Post-COVID}"

* 7. m_j5/e_j3/e2_j3: Uganda should continue allowing refugees who already live in Uganda to work outside the settlements, according to any lockdown rules, during coronavirus.
	replace d1_7 = m_j5 	if wave == 1
	replace d1_7 = e_j3 	if wave == 2
	replace d1_7 = e2_j3 	if wave == 4	
	la var d1_7 "\shortstack{Right to\\Work\\Current}"

	gen d1_7_strongagree = (d1_7 == 1) 		if d1_7 != .
	gen d1_7_strongdisagree = (d1_7 == 5) 	if d1_7 != .
	la var d1_7_strongagree "\shortstack{Strongly\\Supports\\Right to\\Work$^+$}"
	la var d1_7_strongdisagree "\shortstack{Strongly\\Opposes\\Right to\\Work$^+$}"
	
* 8. m_j7/e_j11/ : After coronavirus ends, Uganda should continue allowing refugees to work outside the settlements.
	replace d1_8 = m_j7 	if wave == 1
	replace d1_8 = e_j11 	if wave == 2
	la var d1_8 "\shortstack{Right to\\Work\\Post-COVID}"

* 9. /e_j4/e2_j4: Uganda should continue to provide land to refugees in the settlements.
	replace d1_9 = e_j4			if wave == 2
	replace d1_9 = e2_j4		if wave == 4
	la var d1_9 "\shortstack{Provide\\Land in\\Settlements}"

* 10. /e_j7/e2_j7: For refugees who are already in Uganda, which option do you think Uganda should follow?
	replace d1_10 = (e_j7 <= 2) 	if wave == 2 & (e_j7 != .)
	replace d1_10 = (e2_j7 <= 2) 	if wave == 4 & (e2_j7 != .)
	la var d1_10 "\shortstack{Provide\\Indef\\Stay}"
	
* 11. /e_j5/e2_j5: Uganda should allow refugees to become full citizens if they have lived in Uganda for a long time and would like to become a Ugandan. As citizens, they would have the right to vote in Ugandan elections.
	replace d1_11 = e_j5	if wave == 2
	replace d1_11 = e2_j5	if wave == 4
	la var d1_11 "\shortstack{Provide\\Citizen-\\Ship}"
	
* 12. / /e2_j6n: Uganda recently accepted 2,000 refugees from Afghanistan who were fleeing violence. Do you agree or disagree that Uganda should allow more Afghan refugees to come to Uganda if they would like to?
	replace d1_12 = e2_j6n 	if wave == 4
	la var d1_12 "\shortstack{More\\Afghanistan\\Refugees}"
	
* 13. / /e2_j13: Uganda should continue allowing refugees who live in Uganda right now to work outside the settlements, including in Kampala. Again, please tell me whether you think most of your friends would agree or disagree with this statement.
	replace d1_13 = e2_j13 	if wave == 4
	replace d1_13 = . 		if d1_13 == 7 & wave == 4 // Don't know/Refuse
	la var d1_13 "\shortstack{Freedom of\\Movement\\Friends}"
	
* 14. / /e2_j14: Uganda should accept more refugees. Again, please tell me whether you think most of your friends would agree or disagree with this statement.
	replace d1_14 = e2_j14 	if wave == 4
	replace d1_14 = . 		if d1_14 == 7 & wave == 4 // Don't know/Refuse	
	la var d1_14 "\shortstack{More\\Refugees\\Friends}"

foreach i of numlist 1 2 3 4 5 7 8 9 11 12 13 14 {
	replace d1_`i' = 3 	if d1_`i' == 6
	forval wv = 1/4 {
		qui count if d1_`i' > 3 & d1_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d1_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d1_`i' = (d1_`i' <= 3)	if wave == `wv' & d1_`i' != . & `num_low' < `num_high'
			replace d1_`i' = (d1_`i' < 3) 	if wave == `wv' & d1_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if agree; = 0 if disagree

* 5. Reallocate refugees: 
/* 	wave 1-3: 	1: agree w/ reallocation; 		0: disagree w/ reallocation
	wave 4: 	1: disagree w/ reallocation; 	0: agree w/ reallocation */
	replace d1_5 = 1 - d1_5 	if wave == 4 // now wave 4: 1: agree w/ reallocation; 0: disagree w/ reallocation
		gen d1_5_tmp = 1 - d1_5 // now wave 1-4: 1: disagree w/ reallocation (freedom, pro-refugee); 0: agree w/ reallocation
		drop d1_5
	rename d1_5_tmp d1_5 // disagree w relocation means support
	la var d1_5 "\shortstack{Freedom of\\Movement\\Current}"

rename (d1_9 d1_10 d1_11 d1_12 d1_13 d1_14) (d1a_9 d1a_10 d1a_11 d1a_12 d1a_13 d1a_14) // Not included in PAP


**** Domain 2: Business profits // 1 outcome
gen d2_1 = .
la var d2_1 "\shortstack{Profit\\(IHS)}"

* 1. m_b5/e_b30/p2_b30/e2_b30 : What were the profits of your business during the last 30 days?
* midline
	replace d2_1 = m_b5 	if wave == 1
	replace d2_1 = 0 		if wave == 1 & m_bic1 == 2 & m_bic13 == 2 // no business
	replace d2_1 = d2_1*1000 if wave == 1
	replace d2_1 = 12500    if wave == 1 & m_b5range == 2 & m_b5 == . 
	qui sum d2_1 			if wave == 1 & d2_1 <= 0, de
	replace d2_1 = `r(p50)' if wave == 1 & m_b5range == 1 & m_b5 == . // None or negative

* endline
	replace d2_1 = e_b30    if wave == 2
	replace d2_1 = 0 		if wave == 2 & e_b1 == 2 // no business
	// No individual reported the range without the exact amount

* phone 2
	replace d2_1 = p2_b30	if wave == 3
	replace d2_1 = 0 		if wave == 3 & p2_b1 == 2 // no business
	replace d2_1 = 375000	if wave == 3 & p2_b31 == 3 & p2_b30 == .
	qui sum d2_1 			if wave == 3 & d2_1 <= 0, de
	replace d2_1 = `r(p50)' if wave == 3 & p2_b31 == 1 & p2_b30 == . // None or negative

* endline2 
	replace d2_1 = e2_b30 	if wave == 4
	replace d2_1 = 0 		if wave == 4 & e2_b1 == 2 // no business
	// No individual reported the range without the exact amount

	
**** Domain 3: Knowledge of refugees and hosting policy // 3 outcomes
forval i = 1/3 {
	gen d3_`i' = .
}

* 1. /e_f9/e2_f9: How many refugees in Uganda live outside of camps or settlements: all, most, some, few, or none? ("Some" or "few" will be considered correct answers)
	replace d3_1 = 1 	if wave == 2 & (e_f9 == 3 | e_f9 == 4)
	replace d3_1 = 0 	if wave == 2 & (e_f9 == 1 | e_f9 == 2 | e_f9 == 5 | e_f9 == .)

	replace d3_1 = 1 	if wave == 4 & (e2_f9 == 3 | e2_f9 == 4)
	replace d3_1 = 0 	if wave == 4 & (e2_f9 == 1 | e2_f9 == 2 | e2_f9 == 5 | e2_f9 == .)
	la var d3_1 "\shortstack{Live\\Outside\\Settlements}"

* 2. /e_f10/e2_f10: Are refugees allowed to live outside of the camps or settlements? ("yes" is correct)
	replace d3_2 = 1 	if wave == 2 & e_f10 == 1
	replace d3_2 = 0 	if wave == 2 & (e_f10 == 2 | e_f10 == .)

	replace d3_2 = 1 	if wave == 4 & e2_f10 == 1
	replace d3_2 = 0 	if wave == 4 & (e2_f10 == 2 | e2_f10 == .)
	la var d3_2 "\shortstack{Allowed\\Outside\\Settlements}"

* 3. m_g4/e_f11/e2_f12: Are any of the international donations to refugees in Uganda shared with Ugandans? ("yes" is correct)
	replace d3_3 = 1 	if wave == 1 & m_g4 == 1
	replace d3_3 = 0 	if wave == 1 & (m_g4 == 2 | m_g4 == .)

	replace d3_3 = 1 	if wave == 2 & e_f11 == 1
	replace d3_3 = 0 	if wave == 2 & (e_f11 == 2 | e_f11 == .)

	replace d3_3 = 1 	if wave == 4 & e2_f12 == 1
	replace d3_3 = 0 	if wave == 4 & (e2_f12 == 2 | e2_f12 == .)
	la var d3_3 "\shortstack{Knows\\About\\Aid-Sharing}"


**** Domain 4: Beliefs about economic effects of refugees // 4 outcomes
forval i = 1/4 {
	gen d4_`i' = .
}

* 1. /e_c23/e2_c23: How do the [sector] businesses managed by people from other countries affect your business overall? Do they help you a lot, help you a little, hurt you a little, hurt you a lot, or have no effect on you?
	gen e_c21_tmp = e_c21 
	replace e_c21_tmp = 3 	if e_c15 == 5
	replace d4_1 = 1		if e_c23 <= e_c21_tmp & e_c23 != . & e_c21_tmp !=. & wave == 2 // compare ugandan business influence w/ other country business influence
	replace d4_1 = 0 		if e_c23 > e_c21_tmp & e_c23 != . & e_c21_tmp !=. & wave == 2
	drop e_c21_tmp

	gen e2_c21_tmp = e2_c21 
	replace e2_c21_tmp = 3 	if e2_c15 == 5
	replace d4_1 = 1		if e2_c23 <= e2_c21_tmp & e2_c23 != . & e2_c21_tmp !=. & wave == 4
	replace d4_1 = 0 		if e2_c23 > e2_c21_tmp & e2_c23 != . & e2_c21_tmp !=. & wave == 4
	drop e2_c21_tmp

	la var d4_1 "\shortstack{Pos Effect\\on Your\\Business}"

* 2. m_g1/e_g1/e2_g1: Taking everything into consideration, would you say the overall economic effect of refugees on Uganda has been positive, negative, or neutral?
	replace d4_2 = m_g1 	if wave == 1
	replace d4_2 = e_g1 	if wave == 2
	replace d4_2 = e2_g1 	if wave == 4
	la var d4_2 "\shortstack{Pos Effect\\on Economy\\Overall}"

* 3. m_g2/e_g2/e2_g2: How about the overall economic effect of refugees on you personally?
	replace d4_3 = m_g2 	if wave == 1
	replace d4_3 = e_g2 	if wave == 2
	replace d4_3 = e2_g2 	if wave == 4
	la var d4_3 "\shortstack{Pos Effect\\on You\\Personally}"

* 4. /e_f16/e2_f19: How many refugees have skills and contribute to the economy?
	replace d4_4 = e_f16 	if wave == 2
	replace d4_4 = e_f16x 	if wave == 2 & d4_4 == .
	replace d4_4 = e2_f19 	if wave == 4
	la var d4_4 "\shortstack{Refugees\\Have\\Skills}"

foreach i of numlist 2 3 4 {
	forval wv = 1/4 {
		qui count if d4_`i' > 3 & d4_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d4_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d4_`i' = (d4_`i' <= 3)	if wave == `wv' & d4_`i' != . & `num_low' < `num_high'
			replace d4_`i' = (d4_`i' < 3) 	if wave == `wv' & d4_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative

**** Domain 4a: Beliefs about economic effects of Congolese refugees // 4 outcomes
forval i = 1/4 {
	gen d4a_`i' = .
}

* 1. /e_g7/e2_g7: Taking everything into consideration, would you say the overall economic effect of Congolese on Uganda has been positive, negative, or neutral?
	replace d4a_1 = e_g7	if wave == 2
		qui count if d4a_1 > 3 & d4a_1 != . & wave == 2
		local num_high = r(N)
		qui count if d4a_1 < 3 & wave == 2
		local num_low = r(N)
	replace d4a_1 = (d4a_1 <= 3)	if wave == 2 & d4a_1 != . & `num_low' < `num_high'
	replace d4a_1 = (d4a_1 < 3) 	if wave == 2 & d4a_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	
	replace d4a_1 = e2_g7	if wave == 4
		qui count if d4a_1 > 3 & d4a_1 != . & wave == 4
		local num_high = r(N)
		qui count if d4a_1 < 3 & wave == 4
		local num_low = r(N)
	replace d4a_1 = (d4a_1 <= 3)	if wave == 4 & d4a_1 != . & `num_low' < `num_high'
	replace d4a_1 = (d4a_1 < 3) 	if wave == 4 & d4a_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	la var d4a_1 "\shortstack{Overall\\Economy}" 

* 2. /e_g19/e2_g19: How have access and quality of schools and health facilities been affected by Congolese in Kampala?
	replace d4a_2 = e_g19_tmp 	if wave == 2
	replace d4a_2 = e2_g19_tmp 	if wave == 4
	la var d4a_2 "\shortstack{Schools \&\\Healthcare}" 

* 3. /e_g20/e2_g20: How have rents been affected by Congolese in Kampala?
	replace d4a_3 = e_g20_tmp 	if wave == 2
	replace d4a_3 = e2_g20_tmp 	if wave == 4
	la var d4a_3 "\shortstack{Prices\\Rent}" 

* 4. /e_g21/e2_g21: How have prices of goods you buy, other than rents, been affected by Congolese in Kampala?
	replace d4a_4 = e_g21_tmp 	if wave == 2
	replace d4a_4 = e2_g21_tmp 	if wave == 4
	la var d4a_4 "\shortstack{Prices\\Other Goods}" 

foreach i of numlist 2 3 4 {
	forval wv = 1/4 {
		qui count if d4a_`i' > 2 & d4a_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d4a_`i' < 2 & wave == `wv'
		local num_low = r(N)
			replace d4a_`i' = (d4a_`i' <= 2)	if wave == `wv' & d4a_`i' != . & `num_low' < `num_high'
			replace d4a_`i' = (d4a_`i' < 2) 	if wave == `wv' & d4a_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative


**** Domain 4b: Beliefs about economic effects of Somali refugees // 4 outcomes
forval i = 1/4 {
	gen d4b_`i' = .
}

* 1. /e_g9/e2_g9: Taking everything into consideration, would you say the overall economic effect of Somalis on Uganda has been positive, negative, or neutral?
	replace d4b_1 = e_g9	if wave == 2
		qui count if d4b_1 > 3 & d4b_1 != . & wave == 2
		local num_high = r(N)
		qui count if d4b_1 < 3 & wave == 2
		local num_low = r(N)
			replace d4b_1 = (d4b_1 <= 3)	if wave == 2 & d4b_1 != . & `num_low' < `num_high'
			replace d4b_1 = (d4b_1 < 3) 	if wave == 2 & d4b_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	
	replace d4b_1 = e2_g9	if wave == 4
		qui count if d4b_1 > 3 & d4b_1 != . & wave == 4
		local num_high = r(N)
		qui count if d4b_1 < 3 & wave == 4
		local num_low = r(N)
			replace d4b_1 = (d4b_1 <= 3)	if wave == 4 & d4b_1 != . & `num_low' < `num_high'
			replace d4b_1 = (d4b_1 < 3) 	if wave == 4 & d4b_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	
	la var d4b_1 "\shortstack{Overall\\Economy}" 

* 2. /e_g14/e2_g14: How have access and quality of schools and health facilities been affected by Somalis in Kampala?
	replace d4b_2 = e_g14_tmp 	if wave == 2
	replace d4b_2 = e2_g14_tmp 	if wave == 4
	la var d4b_2 "\shortstack{Schools \&\\Healthcare}" 

* 3. /e_g15/e2_g15: How have rents been affected by Somalis in Kampala?
	replace d4b_3 = e_g15_tmp 	if wave == 2
	replace d4b_3 = e2_g15_tmp 	if wave == 4
	la var d4b_3 "\shortstack{Prices\\Rent}" 

* 4. /e_g16/e2_g16: How have prices of goods you buy, other than rents, been affected by Somalis in Kampala?
	replace d4b_4 = e_g16_tmp 	if wave == 2
	replace d4b_4 = e2_g16_tmp 	if wave == 4
	la var d4b_4 "\shortstack{Prices\\Other Goods}" 

foreach i of numlist 2 3 4 {
	forval wv = 1/4 {
			qui count if d4b_`i' > 2 & d4b_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d4b_`i' < 2 & wave == `wv'
		local num_low = r(N)
			replace d4a_`i' = (d4b_`i' <= 2)	if wave == `wv' & d4b_`i' != . & `num_low' < `num_high'
			replace d4a_`i' = (d4b_`i' < 2) 	if wave == `wv' & d4b_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative


**** Domain 5a: Beliefs that refugees receive too much aid // 2 outcomes
forval i = 1/2 {
	gen d5a_`i' = .
}

* 1. /e_f12/e2_f15: How many refugees have a lot of money? All, most, some, few, or none?
	replace d5a_1 = e_f12 	if wave == 2
	replace d5a_1 = e_f12x 	if wave == 2 & d5a_1 == .
	replace d5a_1 = e2_f15 	if wave == 4
	la var d5a_1 "\shortstack{Have\\Money}" 

* 2. /e_f14/e2_f17: How many refugees get more assistance than they need?/How many refugees deserve support in the form of assistance? 
	replace d5a_2 = e_f14 	if wave == 2
	replace d5a_2 = e_f14x 	if wave == 2 & d5a_2 == .
	replace d5a_2 = e2_f17 	if wave == 4
	la var d5a_2 "\shortstack{Receive\\More Aid\\Than Needed}" 

foreach i of numlist 1 2 {
	forval wv = 1/4 {
		qui count if d5a_`i' > 3 & d5a_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d5a_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d5a_`i' = (d5a_`i' <= 3)	if wave == `wv' & d5a_`i' != . & `num_low' < `num_high'
			replace d5a_`i' = (d5a_`i' < 3) 	if wave == `wv' & d5a_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if too much aid; = 0 if not too much aid

**** Domain 5b: Beliefs that refugees can support themselves // 1 outcome
gen d5b_1 = .

* 1. /e_f15/e2_f18: How many refugees are able to support themselves financially without assistance?
	replace d5b_1 = e_f15 	if wave == 2
	replace d5b_1 = e_f15x 	if wave == 2 & d5b_1 == .
		qui count if d5b_1 > 3 & d5b_1 != . & wave == 2
		local num_high = r(N)
		qui count if d5b_1 < 3 & wave == 2
		local num_low = r(N)
			replace d5b_1 = (d5b_1 <= 3)	if wave == 2 & d5b_1 != . & `num_low' < `num_high'
			replace d5b_1 = (d5b_1 < 3) 	if wave == 2 & d5b_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	
	replace d5b_1 = e2_f18 	if wave == 4
		qui count if d5b_1 > 3 & d5b_1 != . & wave == 4
		local num_high = r(N)
		qui count if d5b_1 < 3 & wave == 4
		local num_low = r(N)
			replace d5b_1 = (d5b_1 <= 3)	if wave == 4 & d5b_1 != . & `num_low' < `num_high'
			replace d5b_1 = (d5b_1 < 3) 	if wave == 4 & d5b_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree

	la var d5b_1 "\shortstack{Can\\Support\\Themselves}"  


**** Domain 6: Cultural attitudes about refugees // 7 outcomes
forval i = 1/7 {
	gen d6_`i' = .
}

* 1. m_g3/e_g3/e2_g3: What effect have refugees had on culture in Uganda?
	replace d6_1 = m_g3 	if wave == 1
	replace d6_1 = e_g3 	if wave == 2
	replace d6_1 = e2_g3 	if wave == 4
	la var d6_1 "\shortstack{Positive\\Effect on\\Culture}" 

* 2. /e_k5/e2_k5: I would be comfortable marrying a refugee.
* 2. /e_k6/e2_k6: I would be comfortable having a refugee marry a member of my family.
* 2. /e_k7/e2_k7: I would be comfortable having a refugee as a close, personal friend.
* 2. /e_k8/e2_k8: I would be comfortable having a refugee as a neighbor.
	gen d6_2_tmp1 = e_k5 	if wave == 2
	gen d6_2_tmp2 = e_k6 	if wave == 2
	gen d6_2_tmp3 = e_k7 	if wave == 2
		replace d6_2_tmp3 = 1 	if e_k5 == 1 & e_k6 == 1
	gen d6_2_tmp4 = e_k8 	if wave == 2
		replace d6_2_tmp4 = 1 	if e_k5 == 1 & e_k6 == 1

	replace d6_2_tmp1 = e2_k5 	if wave == 4
	replace d6_2_tmp2 = e2_k6 	if wave == 4
	replace d6_2_tmp3 = e2_k7 	if wave == 4
		replace d6_2_tmp3 = 1 		if e2_k5 == 1 & e2_k6 == 1
	replace d6_2_tmp4 = e2_k8 	if wave == 4
		replace d6_2_tmp4 = 1 		if e2_k5 == 1 & e2_k6 == 1
		
	la var d6_2_tmp1 "\shortstack{Comfortable\\Refugee\\Spouse}" 
	la var d6_2_tmp2 "\shortstack{Comfortable\\Refugee Spouse\\Family Member}" 
	la var d6_2_tmp3 "\shortstack{Comfortable\\Refugee\\Friends}" 
	la var d6_2_tmp4 "\shortstack{Comfortable\\Refugee\\Neighbors}" 	

* 3. /e_f13/e2_f16: How many refugees deserve sympathy and support?/How many refugees deserve sympathy?
	replace d6_3 = e_f13 	if wave == 2
	replace d6_3 = e_f13x 	if wave == 2 & d6_3 == .
	replace d6_3 = e2_f16 	if wave == 4
	la var d6_3 "\shortstack{Refugees\\Deserve\\Sympathy}" 

foreach i of numlist 1 3 {
	forval wv = 1/4 {
		qui count if d6_`i' > 3 & d6_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d6_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d6_`i' = (d6_`i' <= 3)	if wave == `wv' & d6_`i' != . & `num_low' < `num_high'
			replace d6_`i' = (d6_`i' < 3) 	if wave == `wv' & d6_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
}

* Social proximity index
foreach i of numlist 1 2 3 4 {
	replace d6_2_tmp`i' = 3 	if d6_2_tmp`i' == 6
	forval wv = 1/4 {
		qui count if d6_2_tmp`i' > 3 & d6_2_tmp`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d6_2_tmp`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d6_2_tmp`i' = (d6_2_tmp`i' <= 3)	if wave == `wv' & d6_2_tmp`i' != . & `num_low' < `num_high'
			replace d6_2_tmp`i' = (d6_2_tmp`i' < 3) 	if wave == `wv' & d6_2_tmp`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if agree; = 0 if disagree

	swindex d6_2_tmp1 d6_2_tmp2 d6_2_tmp3 d6_2_tmp4, generate(d6_2) normby(control) replace
	la var d6_2 "\shortstack{Social\\Proximity\\Index}" 

* 4. m_n1/e_n1: Our research team has an extra UGX available. We can give it to you or share it between you and two charity organizations in Uganda. The first charity helps poor Ugandans living in Kampala. The second charity  helps refugees living in Kampala. We are going to let you decide how to split the money. How much of the UGX should we give to the charity supporting poor Ugandans in Kampala?
* 5. m_n2/e_n2: How much of the remaining UGX should we give to the charity supporting refugees in Kampala?

	replace d6_4 = (m_n2)/1500  if wave == 1
	replace d6_4 = (e_n2)/3000  if wave == 2
	replace d6_4 = (e2_n2)/3000 if wave == 4
	la var d6_4 "\shortstack{Prop.\\Donated\\Refugees}" 

	replace d6_5 = (m_n1)/1500  if wave == 1
	replace d6_5 = (e_n1)/3000  if wave == 2
	replace d6_5 = (e2_n1)/3000 if wave == 4
	la var d6_5 "\shortstack{Prop.\\Donated\\Ugandans}"

	gen d6c_4 = (m_n2 > m_n1)		    if wave == 1 & m_n1 != . & m_n2 != .
	replace d6c_4 = (e_n2 > e_n1) 		if wave == 2 & e_n1 != . & e_n2 != .
	replace d6c_4 = (e2_n2 > e2_n1) 	if wave == 4 & e2_n1 != . & e2_n2 != .
	la var d6c_4 "\shortstack{Donation\\Refugees $>$\\Ugandans}"

	gen d6c_5 = (m_n2 >= m_n1) 			if wave == 1 & m_n1 != . & m_n2 != .
	replace d6c_5 = (e_n2 >= e_n1) 		if wave == 2 & e_n1 != . & e_n2 != .
	replace d6c_5 = (e2_n2 >= e2_n1)	if wave == 4 & e2_n1 != . & e2_n2 != .
	la var d6c_5 "\shortstack{Donation\\Refugees $\geq$\\Ugandans}"

* 6. /e_e14/e2_e14: How safe do you feel walking around areas in Kampala where people from other countries live? You can say very safe, somewhat safe, neutral, somewhat unsafe, very unsafe, or that it depends on the nationality.
	gen e_e14_tmp = e_e14
	replace e_e14_tmp = 5 	if e_e14 == 6 & (e_e15_2 == 1 | e_e15_4 == 1) // 2: Congolese; 4: Somali
	replace e_e14_tmp = 3 	if e_e14 == 6 & e_e15_2 != 1 & e_e15_4 != 1
	replace d6_6 = 1 		if e_e14_tmp <= e_e13 & e_e13 != . & e_e14_tmp !=. & wave == 2 // compare safety around foreigners w/ safety in general
	replace d6_6 = 0 		if e_e14_tmp > e_e13 & e_e13 != . & e_e14_tmp !=. & wave == 2
	drop e_e14_tmp

	gen e2_e14_tmp = e2_e14
	replace e2_e14_tmp = 5 	if e2_e14 == 6 & (e2_e15_2 == 1 | e2_e15_4 == 1)
	replace e2_e14_tmp = 3 	if e2_e14 == 6 & e2_e15_2 != 1 & e2_e15_4 != 1
	replace d6_6 = 1 		if e2_e14_tmp <= e2_e13 & e2_e13 != . & e2_e14_tmp !=. & wave == 4
	replace d6_6 = 0 		if e2_e14_tmp > e2_e13 & e2_e13 != . & e2_e14_tmp !=. & wave == 4
	drop e2_e14_tmp

	la var d6_6 "\shortstack{Feel Safe\\in Areas w\\Foreigners}" 

* 7. /e_e17/e2_e17: Is there tension between Ugandans and people from other nationalities?
	replace d6_7 = 1 		if wave == 2 & e_e17 == 2
	replace d6_7 = 0 		if wave == 2 & e_e17 == 1
	replace d6_7 = 1 		if wave == 4 & e2_e17 == 2
	replace d6_7 = 0 		if wave == 4 & e2_e17 == 1
	la var d6_7 "\shortstack{No Tension\\with\\Foreigners}" 

	
**** Domain 6a: Social attitudes about Congolese refugees // 4 outcomes
forval i = 1/4 {
	gen d6a_`i' = .
}

* 1. /e_k9/e2_k9: I would be comfortable marrying a Congolese.
* 1. /e_k10/e2_k10: I would be comfortable having a Congolese marry a member of my family.
* 1. /e_k11/e2_k11: I would be comfortable having a Congolese as a close, personal friend.
* 1. /e_k12/e2_k12: I would be comfortable having a Congolese as a neighbor.
	gen d6a_1_tmp1 = e_k9 	if wave == 2
	gen d6a_1_tmp2 = e_k10 	if wave == 2
	gen d6a_1_tmp3 = e_k11 	if wave == 2
		replace d6a_1_tmp3 = 1 	if e_k9 == 1 & e_k10 == 1
	gen d6a_1_tmp4 = e_k12 	if wave == 2
		replace d6a_1_tmp4 = 1 	if e_k9 == 1 & e_k10 == 1

	replace d6a_1_tmp1 = e2_k9 		if wave == 4
	replace d6a_1_tmp2 = e2_k10 	if wave == 4
	replace d6a_1_tmp3 = e2_k11 	if wave == 4
		replace d6a_1_tmp3 = 1 			if e2_k9 == 1 & e2_k10 == 1
	replace d6a_1_tmp4 = e2_k12 	if wave == 4
		replace d6a_1_tmp4 = 1 			if e2_k9 == 1 & e2_k10 == 1

	la var d6a_1_tmp1 "\shortstack{Comfortable\\Congolese\\Spouse}" 
	la var d6a_1_tmp2 "\shortstack{Comfortable\\Congolese Marry\\Family Member}" 
	la var d6a_1_tmp3 "\shortstack{Comfortable\\Congolese\\Friends}" 
	la var d6a_1_tmp4 "\shortstack{Comfortable\\Congolese\\Neighbors}" 
	
* 2. /e_g8/e2_g8: What effect have Congolese had on culture in Uganda?
	replace d6a_2 = e_g8 	if wave == 2
	replace d6a_2 = e2_g8 	if wave == 4
	la var d6a_2 "\shortstack{Pos Effect\\Culture}" 

* 3. /e_g17/e2_g17: Please tell us how the dress code has been affected by Congolese in Kampala. You can answer positive, negative, or no effect.
	replace d6a_3 = e_g17_tmp 	if wave == 2
	replace d6a_3 = e2_g17_tmp 	if wave == 4
	la var d6a_3 "\shortstack{Pos Effect\\Dress Code}" 

* 4. /e_g18/e2_g18: How have acceptable behaviors (such as how people talk to each other) been affected by Congolese in Kampala?
	replace d6a_4 = e_g18_tmp 	if wave == 2
	replace d6a_4 = e2_g18_tmp 	if wave == 4
	la var d6a_4 "\shortstack{Pos Effect\\Behaviors}" 

	
**** Domain 6b: Social attitudes about Somali refugees // 4 outcomes
forval i = 1/4 {
	gen d6b_`i' = .
}

* 1. /e_k13/e2_k13: I would be comfortable marrying a Somalis.
* 1. /e_k14/e2_k14: I would be comfortable having a Somalis marry a member of my family.
* 1. /e_k15/e2_k15: I would be comfortable having a Somalis as a close, personal friend.
* 1. /e_k16/e2_k16: I would be comfortable having a Somalis as a neighbor.
	gen d6b_1_tmp1 = e_k13 	if wave == 2
	gen d6b_1_tmp2 = e_k14 	if wave == 2
	gen d6b_1_tmp3 = e_k15 	if wave == 2
		replace d6b_1_tmp3 = 1 	if e_k13 == 1 & e_k14 == 1
	gen d6b_1_tmp4 = e_k16 	if wave == 2
		replace d6b_1_tmp4 = 1 	if e_k13 == 1 & e_k14 == 1

	replace d6b_1_tmp1 = e2_k13 	if wave == 4
	replace d6b_1_tmp2 = e2_k14 	if wave == 4
	replace d6b_1_tmp3 = e2_k15 	if wave == 4
		replace d6b_1_tmp3 = 1 			if e2_k13 == 1 & e2_k14 == 1
	replace d6b_1_tmp4 = e2_k16 	if wave == 4
		replace d6b_1_tmp4 = 1 			if e2_k13 == 1 & e2_k14 == 1

	la var d6b_1_tmp1 "\shortstack{Comfortable\\Somali\\Spouse}" 
	la var d6b_1_tmp2 "\shortstack{Comfortable\\Somali Spouse\\Family Member}" 
	la var d6b_1_tmp3 "\shortstack{Comfortable\\Somali\\Friends}" 
	la var d6b_1_tmp4 "\shortstack{Comfortable\\Somali\\Neighbors}" 
	
* 2. /e_g10/e2_g10: What effect have Somalis had on culture in Uganda?
	replace d6b_2 = e_g10 	if wave == 2
	replace d6b_2 = e2_g10 	if wave == 4
	la var d6b_2 "\shortstack{Pos Effect\\Culture}" 

* 3. /e_g12/e2_g12: Please tell us how the dress code has been affected by Somalis in Kampala. You can answer positive, negative, or no effect.
	replace d6b_3 = e_g12_tmp 	if wave == 2
	replace d6b_3 = e2_g12_tmp 	if wave == 4
	la var d6b_3 "\shortstack{Pos Effect\\Dress Code}" 

* 4. /e_g13/e2_g13: How have acceptable behaviors (such as how people talk to each other) been affected by Somalis in Kampala?
	replace d6b_4 = e_g13_tmp 	if wave == 2
	replace d6b_4 = e2_g13_tmp 	if wave == 4
	la var d6b_4 "\shortstack{Pos Effect\\Behaviors}" 

* Social proximity index
foreach var of varlist d6a_1_tmp1 d6a_1_tmp2 d6a_1_tmp3 d6a_1_tmp4 d6b_1_tmp1 d6b_1_tmp2 d6b_1_tmp3 d6b_1_tmp4 {
	forval wv = 1/4 {
		qui count if `var' > 3 & `var' != . & wave == `wv'
		local num_high = r(N)
		qui count if `var' < 3 & wave == `wv'
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if wave == `wv' & `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if wave == `wv' & `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
}

	swindex d6a_1_tmp1 d6a_1_tmp2 d6a_1_tmp3 d6a_1_tmp4 if wave == 2 | wave == 4, generate(d6a_1) normby(control) replace
	la var d6a_1 "\shortstack{Social\\Proximity\\Index}" 

	swindex d6b_1_tmp1 d6b_1_tmp2 d6b_1_tmp3 d6b_1_tmp4 if wave == 2 | wave == 4, generate(d6b_1) normby(control) replace
	la var d6b_1 "\shortstack{Social\\Proximity\\Index}" 

foreach var of varlist d6a_2 d6b_2 {
	forval wv = 1/4 {
		qui count if `var' > 3 & `var' != . & wave == `wv'
		local num_high = r(N)
		qui count if `var' < 3 & wave == `wv'
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if wave == `wv' & `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if wave == `wv' & `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
}

foreach var of varlist d6a_3 d6a_4 d6b_3 d6b_4 {
	forval wv = 1/4 {
		qui count if `var' > 2 & `var' != . & wave == `wv'
		local num_high = r(N)
		qui count if `var' < 2 & wave == `wv'
		local num_low = r(N)
			replace `var' = (`var' <= 2)	if wave == `wv' & `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 2) 	if wave == `wv' & `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative


**** Domain 7: Contact with refugees by choice // 7 outcomes
forval i = 1/6 {
	gen d7_`i' = .
}

* 1. m_c4/e_c12/e2_c12: How many of your business collaborators are from another country?
	replace d7_1 = 0 		if wave == 1 & m_c1 == 2
	replace d7_1 = m_c4 	if wave == 1 & m_c1 == 1
	replace d7_1 = 0 		if wave == 2 & e_c7 == 2
	replace d7_1 = e_c12 	if wave == 2 & e_c7 == 1
	replace d7_1 = 0 		if wave == 4 & e2_c7 == 2
	replace d7_1 = e2_c12 	if wave == 4 & e2_c7 == 1
	la var d7_1 "\shortstack{Foreign\\Business\\Collaborators}" 

* 2. /e_c14/e2_c14: Would you be open to collaborating with business owners from another country?
	replace d7_2 = 1 	if wave == 2 & e_c12 > 0 & e_c12 != .
	replace d7_2 = 1 	if wave == 2 & e_c14 == 1
	replace d7_2 = 0 	if wave == 2 & e_c14 == 2

	replace d7_2 = 1 	if wave == 4 & e2_c12 > 0 & e2_c12 != .
	replace d7_2 = 1 	if wave == 4 & e2_c14 == 1
	replace d7_2 = 0 	if wave == 4 & e2_c14 == 2
	la var d7_2 "\shortstack{Open to\\Collab w\\Foreigners}" 

* 3. /e_c5/e2_c5: In the last 30 days, have you bought supplies (such as materials for your business), tools, or machines from someone from another country?
	replace d7_3 = 1 	if wave == 2 & e_c5 == 1
	replace d7_3 = 0 	if wave == 2 & e_c5 == 2

	replace d7_3 = 1 	if wave == 4 & e2_c5 == 1
	replace d7_3 = 0 	if wave == 4 & e2_c5 == 2
	la var d7_3 "\shortstack{Foreign\\Suppliers}" 

* 4. /e_b8/e2_b8: Have you ever had an apprentice or person from outside your household at your business who was learning skills but not paid who was from another country? (nationalities?)
	replace d7_4 = 1 	if wave == 2 & e_b8 == 1
	replace d7_4 = 0 	if wave == 2 & e_b8 == 2
	replace d7_4 = 0 	if wave == 2 & e_b5 == 2

	replace d7_4 = 1 	if wave == 4 & e2_b8 == 1
	replace d7_4 = 0 	if wave == 4 & e2_b8 == 2
	replace d7_4 = 0 	if wave == 4 & e2_b5 == 2
	la var d7_4 "\shortstack{Foreign\\Apprentices}" 

* 5. /e_b26/e2_b26: Are any of your employees from a different country than you?
	replace d7_5 = 1 	if wave == 2 & e_b26 == 1
	replace d7_5 = 0 	if wave == 2 & e_b26 == 2
	replace d7_5 = 0 	if wave == 2 & e_b23 == 0

	replace d7_5 = 1 	if wave == 4 & e2_b26 == 1
	replace d7_5 = 0 	if wave == 4 & e2_b26 == 2
	replace d7_5 = 0 	if wave == 4 & e2_b23 == 0
	la var d7_5 "\shortstack{Foreign\\Employees}" 

* 6. /e_e8/e2_e8: In the past 30 days, how many people from another country have you contacted for any social reason, such as having a long conversation?
	replace d7_6 = e_e8 	if wave == 2
	replace d7_6 = e2_e8 	if wave == 4
	la var d7_6 "\shortstack{Foreign\\Contacts}" 

* 7. /BN module/BN module: Number of people from another country listed in the networks module.
	gen d7_7_tmp1 = 1 	if wave == 2 & e_bn4___1 != 1 & e_bn4___1 != .
	gen d7_7_tmp2 = 1 	if wave == 2 & e_bn4___2 != 1 & e_bn4___2 != .
	gen d7_7_tmp3 = 1 	if wave == 2 & e_bn4___3 != 1 & e_bn4___3 != .
	gen d7_7_tmp4 = 1 	if wave == 2 & e_bn4___4 != 1 & e_bn4___4 != .

	replace d7_7_tmp1 = 1 	if wave == 4 & e2_bn4_1 != 1 & e2_bn4_1 != .
	replace d7_7_tmp2 = 1 	if wave == 4 & e2_bn4_2 != 1 & e2_bn4_2 != .
	replace d7_7_tmp3 = 1 	if wave == 4 & e2_bn4_3 != 1 & e2_bn4_3 != .

	egen d7_7 = rowtotal(d7_7_tmp1 d7_7_tmp2 d7_7_tmp3 d7_7_tmp4) 	if (wave == 2 | wave == 4) & phone_survey == 0
	drop d7_7_tmp*
	la var d7_7 "\shortstack{Foreign\\Networks}" 
	

**** Domain 8: Contact with refugees by circumstance // 3 outcomes
forval i = 1/3 {
	gen d8_`i' = .
}

* 1. /e_e6/e2_e6: How many people from other countries live in your neighborhood? Many, some, few, or none?
	replace d8_1 = 1 	if wave == 2 & (e_e6 == 1 | e_e6 == 2)
	replace d8_1 = 0 	if wave == 2 & (e_e6 == 3 | e_e6 == 4)

	replace d8_1 = 1 	if wave == 4 & (e2_e6 == 1 | e2_e6 == 2)
	replace d8_1 = 0 	if wave == 4 & (e2_e6 == 3 | e2_e6 == 4)
	la var d8_1 "\shortstack{Foreigners\\in\\Neighborhood}" 

* 2. /e_c17/e2_c17: How many businesses in your sector in this area are managed by people from another country?
	replace d8_2 = e_c17 	if wave == 2
	replace d8_2 = e2_c17 	if wave == 4
	la var d8_2 "\shortstack{Foreign\\Businesses\\in Area}" 

* 3. /e_c3/e2_c3: How many of your customers are from another country?
	replace d8_3 = e_c3 	if wave == 2
	replace d8_3 = e2_c3 	if wave == 4
	la var d8_3 "\shortstack{Foreign\\Customers}" 

foreach i of numlist 2 3 {
	forval wv = 1/4 {
		qui count if d8_`i' > 3 & d8_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d8_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d8_`i' = (d8_`i' <= 3)	if wave == `wv' & d8_`i' != . & `num_low' < `num_high'
			replace d8_`i' = (d8_`i' < 3) 	if wave == `wv' & d8_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
}


**** Domain 9: Business practices // 10 outcomes
forval i = 1/10 {
	gen d9_`i' = .
}

* 1. /: Is your business officially registered? 

* 2. /e_b15/p2_b15/e2_b15: If you were to sell all the business-related equipment you own right now (such as chairs, machines and tools), how much do you think you could make?
* 2. /e_b17/p2_b17/e2_b17: If you were to sell all the inventory you own right now (e.g. fabric, thread, soap), how much do you think you could make?
	gen d9_2_tmp1 = e_b15 				 	if wave == 2 
	replace d9_2_tmp1 = 875000		 	 	if wave == 2 & e_b16 == 4 
	replace d9_2_tmp1 = 1750000		 	 	if wave == 2 & e_b16 == 6 
	replace d9_2_tmp1 = 2500000		 	 	if wave == 2 & e_b16 == 7 
	gen d9_2_tmp2 = e_b17 				 	if wave == 2
	qui sum d9_2_tmp2 						if wave == 2 & e_b17 <= 250000, de
	replace d9_2_tmp2 = `r(p50)'  			if wave == 2 & e_b18 == 1
	replace d9_2_tmp2 = 1750000			 	if wave == 2 & e_b18 == 6
	replace d9_2 = d9_2_tmp1 + d9_2_tmp2 	if wave == 2

	replace d9_2_tmp1 = p2_b15 				if wave == 3
	replace d9_2_tmp1 = 625000				if wave == 3 & p2_b16 == 3
	qui sum d9_2_tmp1 						if wave == 3 & p2_b15 <= 250000, de
	replace d9_2_tmp1 = `r(p50)' 			if wave == 3 & p2_b16 == 1
	replace d9_2_tmp2 = p2_b17  			if wave == 3
	replace d9_2_tmp2 = 625000 				if wave == 3 & p2_b18 == 3
	qui sum p2_b17 							if wave == 3 & p2_b17 <= 250000, de
	replace d9_2_tmp2 = `r(p50)' 			if wave == 3 & p2_b18 == 1
	replace d9_2 = d9_2_tmp1 + d9_2_tmp2 	if wave == 3


	replace d9_2_tmp1 = e2_b15 				if wave == 4
	replace d9_2_tmp1 = 4000000				if wave == 4 & e2_b16 == 8
	replace d9_2_tmp2 = e2_b17  			if wave == 4 // no range update
	replace d9_2 = d9_2_tmp1 + d9_2_tmp2 	if wave == 4

	la var d9_2 "\shortstack{Business\\Capital\\(IHS)}" 

* 3. m_b1/e_b19/p2_b19/e2_b19: Over the past 7 days, how many hours did you work at this business?
	replace d9_3 = m_b1 	if wave == 1
	replace d9_3 = 0 		if wave == 1 & m_bic1 == 2 & m_bic13 == 2
	replace d9_3 = e_b19 	if wave == 2 
	replace d9_3 = 0 		if wave == 2 & e_b1 == 2
	replace d9_3 = p2_b19 	if wave == 3
	replace d9_3 = 0		if wave == 3 & p2_b1 == 2
	replace d9_3 = e2_b19 	if wave == 4
	replace d9_3 = 0		if wave == 4 & e2_b1 == 2

	gen d9_3_stat = .
	forval wv = 1/4 {
		winsor2 d9_3 				if wave == `wv', replace cuts(1 99)
		replace d9_3_stat = d9_3 	if wave == `wv' // for statistics	
		replace d9_3 = asinh(d9_3) 	if wave == `wv'
	}
	la var d9_3 "\shortstack{Working\\Hours\\(IHS)}" 

* 4. /e_b33/e2_b33: In the past year, how many times did you take out a loan for your business?
	replace d9_4 = e_b33 	if wave == 2
	replace d9_4 = e2_b33 	if wave == 4
	replace d9_4 = . 		if d9_4 > 100 // wrongly surveyed
	la var d9_4 "\shortstack{Business\\Loans}" 

* 5. /e_b36/p2_b36/e2_b35: How much total business-related debt do you currently have?
	replace d9_5 = 0 		if wave == 2 & e_b35 == 2
	replace d9_5 = e_b36 	if wave == 2 & e_b35 == 1 // no range update
	replace d9_5 = 0 		if wave == 3 & p2_b35 == 2
	replace d9_5 = p2_b36	if wave == 3 & p2_b35 == 1 // no range update
	replace d9_5 = 0 		if wave == 4 & e2_b35 == 2
	replace d9_5 = e2_b36	if wave == 4 & e2_b35 == 1 // no range update
	la var d9_5 "\shortstack{Business\\Debt\\(IHS)}" 

* 6. /BN module/BN module: Number of contacts listed in the networks module.
	destring e_bn_repeat_count, replace
	replace d9_6 = e_bn_repeat_count 	if wave == 2
	destring e2_bn_repeat_count, replace
	replace d9_6 = e2_bn_repeat_count 	if wave == 4
	la var d9_6 "\shortstack{Business\\Networks}" 

* 7. /e_bp22/e2_bp22: Over the past year, how often did you spend money advertising your business? Every day, every week, every month, a couple times, or never?
	replace d9_7 = e_bp22 	if wave == 2
	replace d9_7 = e2_bp22 	if wave == 4
	la var d9_7 "\shortstack{Marketing}" 

* 8. /e_bp23/e2_bp23: How often did you keep written books/accounting records? Always, frequently, sometimes, occasionally, or never?
	replace d9_8 = e_bp23 	if wave == 2
	replace d9_8 = e2_bp23 	if wave == 4
	la var d9_8 "\shortstack{Record\\Keeping}" 

* 9. /e_bp24/e2_bp24: How often did you sell goods or provide services to customers on credit? For all sales, most sales, some sales, a few sales, or never? 
	replace d9_9 = e_bp24 	if wave == 2
	replace d9_9 = e2_bp24 	if wave == 4
	la var d9_9 "\shortstack{Sell\\on Credit}" 

* 10. /e_bp25/e2_bp25: How often did you buy materials, tools, or machines for your business on credit? For all sales, most sales, some sales, a few sales, or never? 
	replace d9_10 = e_bp25 	if wave == 2
	replace d9_10 = e2_bp25 if wave == 4
	la var d9_10 "\shortstack{Buy\\on Credit}" 

foreach i of numlist 7 8 9 10 {
	forval wv = 1/4 {
		qui count if d9_`i' > 3 & d9_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d9_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d9_`i' = (d9_`i' <= 3)	if wave == `wv' & d9_`i' != . & `num_low' < `num_high'
			replace d9_`i' = (d9_`i' < 3) 	if wave == `wv' & d9_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
}


**** Domain 9a: Business practies (marketing) // 7 outcomes // Endline, Endline2
forval i = 1/7 {
	gen d9a_`i' = .
}

* 1. /e_bp1/e2_bp1: Visited at least one of your competitor's businesses to see what prices your competitors are charging.
	replace d9a_1 = 1 	if wave == 2 & e_bp1 == 1
	replace d9a_1 = 0 	if wave == 2 & (e_bp1 == 2 | e_bp1 == 3)
	replace d9a_1 = 1  	if wave == 4 & e2_bp1 == 1
	replace d9a_1 = 0 	if wave == 4 & (e2_bp1 == 2 | e2_bp1 == 3)
	la var d9a_1 "\shortstack{Check\\Competitor\\Prices}" 

* 2. /e_bp2/e2_bp2: Visited at least one of your competitor's businesses to see what products your competitors have available for sale.
	replace d9a_2 = 1 	if wave == 2 & e_bp2 == 1
	replace d9a_2 = 0 	if wave == 2 & (e_bp2 == 2 | e_bp2 == 3)
	replace d9a_2 = 1 	if wave == 4 & e2_bp2 == 1
	replace d9a_2 = 0 	if wave == 4 & (e2_bp2 == 2 | e2_bp2 == 3)	
	la var d9a_2 "\shortstack{Check\\Competitor\\Products}" 

* 3. /e_bp3/e2_bp3: Asked existing customers whether there are any other products the customers would like the business to sell or produce.
	replace d9a_3 = 1 	if wave == 2 & e_bp3 == 1
	replace d9a_3 = 0 	if wave == 2 & e_bp3 == 2
	replace d9a_3 = 1 	if wave == 4 & e2_bp3 == 1
	replace d9a_3 = 0 	if wave == 4 & e2_bp3 == 2	
	la var d9a_3 "\shortstack{Consult\\Customers\\on Products}" 

* 4. /e_bp4/e2_bp4: Talked with at least one former customer to find out why former customers have stopped buying from your business.
	replace d9a_4 = 1 	if wave == 2 & e_bp4 == 1
	replace d9a_4 = 0 	if wave == 2 & (e_bp4 == 2 | e_bp4 == 3)
	replace d9a_4 = 1 	if wave == 4 & e2_bp4 == 1
	replace d9a_4 = 0 	if wave == 4 & (e2_bp4 == 2 | e2_bp4 == 3)	
	la var d9a_4 "\shortstack{Ask\\Customer\\Who Left}" 

* 5. /e_bp5/e2_bp5: Asked a supplier about which products are selling well in your business' industry.
	replace d9a_5 = 1 	if wave == 2 & e_bp5 == 1
	replace d9a_5 = 0 	if wave == 2 & e_bp5 == 2
	replace d9a_5 = 1 	if wave == 4 & e2_bp5 == 1
	replace d9a_5 = 0 	if wave == 4 & e2_bp5 == 2	
	la var d9a_5 "\shortstack{Ask\\Supplers abt\\Products}" 

* 6. /e_bp6/e2_bp6: Attracted customers with a special offer. 
	replace d9a_6 = 1 	if wave == 2 & e_bp6 == 1
	replace d9a_6 = 0 	if wave == 2 & e_bp6 == 2
	replace d9a_6 = 1 	if wave == 4 & e2_bp6 == 1
	replace d9a_6 = 0 	if wave == 4 & e2_bp6 == 2	
	la var d9a_6 "\shortstack{Give\\Special\\Offers}" 

* 7. /e_bp22/e2_bp22: Over the last 30 days, how often did you spend money advertising your business? Every day, every week, every month, a couple times, or never?
	
	replace d9a_7 = e_bp22 		if wave == 2
	replace d9a_7 = e2_bp22 	if wave == 4
	
	forval wv = 1/4 {
		qui count if d9a_7 > 3 & d9a_7 != . & wave == `wv'
		local num_high = r(N)
		qui count if d9a_7 < 3 & wave == `wv'
		local num_low = r(N)
			replace d9a_7 = (d9a_7 <= 3)	if wave == `wv' & d9a_7 != . & `num_low' < `num_high'
			replace d9a_7 = (d9a_7 < 3) 	if wave == `wv' & d9a_7 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}	

	la var d9a_7 "\shortstack{Spend\\Money\\Advertising}" 

	
**** Domain 9b: Business practies (stock) // 3 outcomes // Endline, Endline2
forval i = 1/3 {
	gen d9b_`i' = .
}

* 1. /e_bp7/e2_bp7: Attempted to negotiate with a supplier for a lower price on raw material.
	replace d9b_1 = 1 	if wave == 2 & e_bp7 == 1
	replace d9b_1 = 0 	if wave == 2 & e_bp7 == 2
	replace d9b_1 = 1 	if wave == 4 & e2_bp7 == 1
	replace d9b_1 = 0 	if wave == 4 & e2_bp7 == 2	
	la var d9b_1 "\shortstack{Negotiate\\Price\\w Supplier}" 

* 2. /e_bp8/e2_bp8: Compared the prices or quality offered by alternate suppliers or sources of raw materials to your business' current suppliers or sources of raw material.
	replace d9b_2 = 1 	if wave == 2 & e_bp8 == 1
	replace d9b_2 = 0 	if wave == 2 & e_bp8 == 2
	replace d9b_2 = 1 	if wave == 4 & e2_bp8 == 1
	replace d9b_2 = 0 	if wave == 4 & e2_bp8 == 2	
	la var d9b_2 "\shortstack{Compare\\btw Suppliers}" 

* 3. /e_bp9/e2_bp9: Ran out of stock or raw materials once per month or more.
	replace d9b_3 = 0 	if wave == 2 & (e_bp9 == 1 | e_bp9 == 3)
	replace d9b_3 = 1 	if wave == 2 & e_bp9 == 2
	replace d9b_3 = 0 	if wave == 4 & (e2_bp9 == 1 | e2_bp9 == 3)
	replace d9b_3 = 1 	if wave == 4 & e2_bp9 == 2	
	la var d9b_3 "\shortstack{Use Up\\Stock}" 

	
**** Domain 9c: Business practies (record-keeping) // 3 outcomes // Endline, Endline2
forval i = 1/3 {
	gen d9c_`i' = .
}

* 1. /e_bp10/e2_bp10: Record every purchase and sale made by your business.
	replace d9c_1 = 1 	if wave == 2 & e_bp10 == 1
	replace d9c_1 = 0 	if wave == 2 & e_bp10 == 2
	replace d9c_1 = 1 	if wave == 4 & e2_bp10 == 1
	replace d9c_1 = 0 	if wave == 4 & e2_bp10 == 2	
	la var d9c_1 "\shortstack{Record\\Purchase \&\\Sale}" 

* 2. /e_bp11/e2_bp11: Have a written budget, which states how much is owed each month for rent, electricity, equipment maintenance, transport, advertising, and other indirect costs to business.
	replace d9c_2 = 1 	if wave == 2 & e_bp11 == 1
	replace d9c_2 = 0 	if wave == 2 & e_bp11 == 2
	replace d9c_2 = 1 	if wave == 4 & e2_bp11 == 1
	replace d9c_2 = 0 	if wave == 4 & e2_bp11 == 2	
	la var d9c_2 "\shortstack{Have\\Written\\Budget}" 

* 3. /e_bp23/e2_bp23: Over the last 30 days, how often did you keep books/accounting records? Always, frequently, sometimes, occasionally, or never?
	
	replace d9c_3 = e_bp23 		if wave == 2
	replace d9c_3 = e2_bp23 	if wave == 4
	
	forval wv = 1/4 {
		qui count if d9c_3 > 3 & d9c_3 != . & wave == `wv'
		local num_high = r(N)
		qui count if d9c_3 < 3 & wave == `wv'
		local num_low = r(N)
			replace d9c_3 = (d9c_3 <= 3)	if wave == `wv' & d9c_3 != . & `num_low' < `num_high'
			replace d9c_3 = (d9c_3 < 3) 	if wave == `wv' & d9c_3 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}	
	
	la var d9c_3 "\shortstack{Keep\\Accounting\\Records}" 


**** Domain 9d: Business practies (composite index of all 13 outcomes)

**** Domain 9e: Business practies (reporting changing practices) // 5 outcomes // Endline
forval i = 1/5 {
	gen d9e_`i' = .
}

* 1. /e_bp12: Compared to before the lockdown, are you buying your business materials from any new suppliers?
	replace d9e_1 = 1 	if wave == 2 & e_bp12 == 1
	replace d9e_1 = 0 	if wave == 2 & e_bp12 == 2
	la var d9e_1 "\shortstack{Change\\Suppliers}" 

* 2. /e_bp14: Compared to before the lockdown, have you made any changes to services that you offer? For example, offering new services, or changing how you conduct an existing service?
	replace d9e_2 = 1 	if wave == 2 & e_bp14 == 1
	replace d9e_2 = 0 	if wave == 2 & e_bp14 == 2
	la var d9e_2 "\shortstack{Change\\Services}" 

* 3. /e_bp16: Compared to before the lockdown, have you made any changes to how you advertise your business?
	replace d9e_3 = 1 	if wave == 2 & e_bp16 == 1
	replace d9e_3 = 0 	if wave == 2 & e_bp16 == 2
	la var d9e_3 "\shortstack{Change\\Ads}" 

* 4. /e_bp18: Compared to before the lockdown, have you made any changes to how you manage your business, such as record keeping, accounting, or managing money?
	replace d9e_4 = 1 	if wave == 2 & e_bp18 == 1
	replace d9e_4 = 0 	if wave == 2 & e_bp18 == 2
	la var d9e_4 "\shortstack{Change\\Business\\Management}" 

* 5. /e_bp20: Compared to before the lockdown, has this business changed its number of employees, number of machines, or space occupied?
	replace d9e_5 = 1 	if wave == 2 & e_bp20 == 1
	replace d9e_5 = 0 	if wave == 2 & e_bp20 == 2
	la var d9e_5 "\shortstack{Change\\Business\\Size}" 

	
**** Domain 10: Household Well-Being // 8 outcomes
forval i = 1/8 {
	gen d10_`i' = .
}

* 1. /e_b30/p2_b30/e2_b30: What were the profits of your business during the last 30 days? 
* 1. /e_d6/p2_d6/e2_d6: What were the profits of [any other household-owned] businesses (excluding this one) during the last 30 days? 
* 1. /e_d9/p2_d9/e2_d9: How much wage income did you earn in the last 30 days? 
* 1. /b_d12/p2_d12/e2_d12: How much wage income did [other members of your household] earn in the last 30 days? 
	* Endline
	gen d10_1_tmp1 = .
	replace d10_1_tmp1 = e_b30 	if wave == 2 // no range updated

	gen d10_1_tmp2 = .
	replace d10_1_tmp2 = e_d6 	if wave == 2
	qui sum d10_1_tmp2 if wave == 2 & d10_1_tmp2 <= 0, de // median in the bottom range
	replace d10_1_tmp2 = `r(p50)' if wave == 2 & e_d7 == 1
	replace d10_1_tmp2 = 87500 	if wave == 2 & e_d7 == 5
	replace d10_1_tmp2 = 175000 if wave == 2 & e_d7 == 7
	replace d10_1_tmp2 = 750000 if wave == 2 & e_d7 == 10
	replace d10_1_tmp2 = 0 		if wave == 2 & e_d5 == 2 // no additional business

	gen d10_1_tmp3 = .
	replace d10_1_tmp3 = e_d9 	if wave == 2 // no range updated
	replace d10_1_tmp3 = 0 		if wave == 2 & e_d8 == 2 // no own wage income

	gen d10_1_tmp4 = .
	replace d10_1_tmp4 = e_d12 	if wave == 2
	qui sum d10_1_tmp4 if wave == 2 & d10_1_tmp4 <= 0, de // median in the bottom range
	replace d10_1_tmp4 = `r(p50)' if wave == 2 & e_d13 == 1
	replace d10_1_tmp4 = 250000 if wave == 2 & e_d13 == 8
	replace d10_1_tmp4 = 400000 if wave == 2 & e_d13 == 9
	replace d10_1_tmp4 = 750000 if wave == 2 & e_d13 == 10
	replace d10_1_tmp4 = 0 		if wave == 2 & e_d11 == 2 // no wage for hh members
	replace d10_1_tmp4 = 0 		if wave == 2 & e_hh_mem_o5 == 1 // no other hh members

	replace d10_1 = d10_1_tmp1 + d10_1_tmp2 + d10_1_tmp3 + d10_1_tmp4 	if wave == 2

	* Phone2
	replace d10_1_tmp1 = p2_b30 if wave == 3 
	replace d10_1_tmp1 = 0		if wave == 3 & p2_b1 == 2 // no business
	replace d10_1_tmp1 = 375000	if wave == 3 & p2_b31 == 3
	qui sum d10_1_tmp1 if wave == 3 & d2_1 <= 0, de
	replace d10_1_tmp1 = `r(p50)' if wave == 3 &  p2_b31 == 1 // None or negative

	replace d10_1_tmp2 = p2_d6 	if wave == 3
	qui sum d10_1_tmp2 if wave == 3 & d10_1_tmp2 <= 0, de // median in the bottom range
	replace d10_1_tmp2 = `r(p50)' if wave == 3 & p2_d7 == 1
	replace d10_1_tmp2 = 175000 if wave == 3 & p2_d7 == 7
	replace d10_1_tmp2 = 250000	if wave == 3 & p2_d7 == 8
	replace d10_1_tmp2 = 750000 if wave == 3 & p2_d7 == 10
	replace d10_1_tmp2 = 0 		if wave == 3 & p2_d5 == 2 // no additional business

	replace d10_1_tmp3 = p2_d9 	if wave == 3 // no range updated
	replace d10_1_tmp3 = 0	 	if wave == 3 & p2_d8 == 2 // no own wage income

	replace d10_1_tmp4 = p2_d12 if wave == 3
	qui sum d10_1_tmp4 if wave == 3 & d10_1_tmp4 <= 0, de // median in the bottom range
	replace d10_1_tmp4 = `r(p50)' if wave == 3 & p2_d13 == 1
	replace d10_1_tmp4 = 750000 if wave == 3 & p2_d13 == 10
	replace d10_1_tmp4 = 0 		if wave == 3 & p2_d11 == 2 // no wage for hh members
	//replace d10_1_tmp4 = 0 		if wave == 3 & p2_hh_mem_o5 == 1 // no this calculated variable in Phone2

	replace d10_1 = d10_1_tmp1 + d10_1_tmp2 + d10_1_tmp3 + d10_1_tmp4 	if wave == 3

	* Endline2
	replace d10_1_tmp1 = e2_b30 	if wave == 4 // no range updated
	replace d10_1_tmp1 = 0			if wave == 4 & e2_b1 == 2 // no business

	replace d10_1_tmp2 = e2_d6 		if wave == 4
	replace d10_1_tmp2 = 175000 	if wave == 4 & e2_d7 == 7
	replace d10_1_tmp2 = 0 			if wave == 4 & e2_d5 == 2 // no additional business

	replace d10_1_tmp3 = e2_d9 		if wave == 4 // no range updated
	replace d10_1_tmp3 = 0 			if wave == 4 & e2_d8 == 2 // no own wage income

	replace d10_1_tmp4 = e2_d12 	if wave == 4
	qui sum d10_1_tmp4 				if wave == 4 & d10_1_tmp4 <= 0, de // median in the bottom range
	replace d10_1_tmp4 = `r(p50)'	if wave == 4 & e2_d13 == 1
	replace d10_1_tmp4 = 400000 	if wave == 4 & e2_d13 == 9
	replace d10_1_tmp4 = 0 			if wave == 4 & e2_d11 == 2 // no wage for hh members
	replace d10_1_tmp4 = 0  		if wave == 4 & e2_hh_mem_o5 == 1 // no other hh members

	replace d10_1 = d10_1_tmp1 + d10_1_tmp2 + d10_1_tmp3 + d10_1_tmp4 	if wave == 4

	la var d10_1 "\shortstack{Total\\Household\\Income (IHS)}" 

* 2. m_bic1/e_b1/p2_b1/e2_b1: Business survival, measured using an indicator for whether the main business is operating at the time of the survey
	replace d10_2 = 1 	if wave == 1 & m_bic1 == 1
	replace d10_2 = 0 	if wave == 1 & m_bic1 == 2
	replace d10_2 = 1 	if wave == 2 & e_b1 == 1
	replace d10_2 = 0 	if wave == 2 & e_b1 == 2
	replace d10_2 = 1	if wave == 3 & p2_b1 == 1
	replace d10_2 = 0	if wave == 3 & p2_b1 == 2
	replace d10_2 = 1	if wave == 4 & e2_b1 == 1
	replace d10_2 = 0	if wave == 4 & e2_b1 == 2
	la var d10_2 "\shortstack{Business\\Survival}" 

* 3. /e_d14/p2_d14/e2_d4: How much money was your household able to save in the past 30 days? 
	replace d10_3 = e_d14 		if wave == 2
	qui sum d10_3 				if wave == 2 & d10_3 <= 0, de
	replace d10_3 = `r(p50)' 	if wave == 2 & e_d15 == 1
	replace d10_3 = p2_d14		if wave == 3
	qui sum d10_3 				if wave == 3 & d10_3 <= 0, de
	replace d10_3 = `r(p50)' 	if wave == 3 & p2_d15 == 1
	replace d10_3 = e2_d14		if wave == 4
	qui sum d10_3 				if wave == 4 & d10_3 <= 0, de
	replace d10_3 = `r(p50)' 	if wave == 4 & e2_d15 == 1
	la var d10_3 "\shortstack{Saving\\(IHS)}" 

* 4. /e_e1/e2_e1: Compared to the average Ugandan in your neighborhood, how would you describe the economic situation of your household? Much better, somewhat better, about the same, somewhat worse, or much worse?
	replace d10_4 = e_e1 	if wave == 2
	replace d10_4 = e2_e1 	if wave == 4
	la var d10_4 "\shortstack{Relative\\Economic\\Situation}" 

forval wv = 1/4 {
		qui count if d10_4 > 3 & d10_4 != . & wave == `wv'
		local num_high = r(N)
		qui count if d10_4 < 3 & wave == `wv'
		local num_low = r(N)
			replace d10_4 = (d10_4 <= 3)	if wave == `wv' & d10_4 != . & `num_low' < `num_high'
			replace d10_4 = (d10_4 < 3) 	if wave == `wv' & d10_4 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if better; = 0 if worse

* 5. m_d1/e_d16/p2_d16/e2_d16: Over the past 30 days, how often have you or anyone in your household gone without enough food to eat? 
	replace d10_5 = m_d1 	if wave == 1
	replace d10_5 = e_d16 	if wave == 2
	replace d10_5 = p2_d16	if wave == 3
	replace d10_5 = e2_d16	if wave == 4
	la var d10_5 "\shortstack{Have Food}" 

* 6. m_d2/e_d17/p2_d17/e2_d17: Over the past 30 days, how often have you or anyone in your household struggled to afford basic household expenses (such as medicine, rent, school fees)? 
	replace d10_6 = m_d2 	if wave == 1
	replace d10_6 = e_d17 	if wave == 2
	replace d10_6 = p2_d17 	if wave == 3
	replace d10_6 = e2_d17 	if wave == 4
	la var d10_6 "\shortstack{Fine w\\Household\\Expenses}" 

foreach i of numlist 5 6 {
	forval wv = 1/4 {
		qui sum d10_`i' 	if wave == `wv', de
		if `r(N)' != 0 {
			replace d10_`i' = (d10_`i' >= 3) 	if wave == `wv' & d10_`i' != .
		}
	}
} // = 1 if sometimes or never; = 0 if always or often

* 7. /e_d18/p2_d18/e2_d18: In the past 30 days, have you or anyone in your household had to sell assets (jewelry, furniture, clothing, tools, machines, land) in order to afford basic household expenses? 
	replace d10_7 = 1 	if wave == 2 & e_d18 == 2
	replace d10_7 = 0 	if wave == 2 & e_d18 == 1 // = 1 if no; = 0 if yes
	replace d10_7 = 1 	if wave == 3 & p2_d18 == 2
	replace d10_7 = 0 	if wave == 3 & p2_d18 == 1 // = 1 if no; = 0 if yes
	replace d10_7 = 1 	if wave == 4 & e2_d18 == 2
	replace d10_7 = 0 	if wave == 4 & e2_d18 == 1 // = 1 if no; = 0 if yes
	la var d10_7 "\shortstack{No Need\\to Sell\\Assets}" 

* 8. /e_d19/e2_d19: In the past 30 days, has your household had to stop education for a child due to lack of finances? 
	replace d10_8 = 1 	if wave == 2 & e_d19 == 2
	replace d10_8 = 0 	if wave == 2 & e_d19 == 1 // = 1 if no; = 0 if yes; missing if NA
	replace d10_8 = 1 	if wave == 3 & p2_d19 == 2
	replace d10_8 = 0 	if wave == 3 & p2_d19 == 1 // = 1 if no; = 0 if yes; missing if NA
	replace d10_8 = 1 	if wave == 4 & e2_d19 == 2
	replace d10_8 = 0 	if wave == 4 & e2_d19 == 1 // = 1 if no; = 0 if yes; missing if NA
	la var d10_8 "\shortstack{Can Afford\\Child\\Education}" 

	
**** Domain 11: Policy Preferences and Representation // 4 outcomes
forval i = 1/4 {
	gen d11_`i' = .
}

* 1. /e_j8/e2_j8: Do you agree or disagree with the following statement: Uganda should accept more foreigners besides refugees. 
	replace d11_1 = e_j8 	if wave == 2
	replace d11_1 = e2_j8 	if wave == 4

replace d11_1 = 3 	if d11_1 == 6 // no opinion / don't know / decline is coded as missing
forval wv = 1/4 {
		qui count if d11_1 > 3 & d11_1 != . & wave == `wv'
		local num_high = r(N)
		qui count if d11_1 < 3 & wave == `wv'
		local num_low = r(N)
			replace d11_1 = (d11_1 <= 3)	if wave == `wv' & d11_1 != . & `num_low' < `num_high'
			replace d11_1 = (d11_1 < 3) 	if wave == `wv' & d11_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
}
la var d11_1 "\shortstack{Immigrants:\\Accept\\More}" 
	
* 2. /e_j9/e2_j9: For foreigners, besides refugees, which option do you think Uganda should follow? (analyzed as 4 binary variables)

	replace d11_2 = (e_j9 == 1)		if wave == 2 & e_j9 != .
	replace d11_2 = (e2_j9 == 1)	if wave == 4 & e2_j9 != .
	la var d11_2 "\shortstack{Immigrants:\\Allow\\To Stay}" 

* 3. /e_c24/p2_c24/e2_d22: How satisfied are you with the LC1 for this area? 
	replace d11_3 = e_c24 	if wave == 2
	replace d11_3 = p2_c24	if wave == 3
	replace d11_3 = e2_d22	if wave == 4
	la var d11_3 "\shortstack{Satisfied\\w Local\\Politician}" 

* 4. /e_c25/p2_c25/e2_d23: How satisfied are you with the MP for this area? 
	replace d11_4 = m_cov23 if wave == 1
	replace d11_4 = e_c25 	if wave == 2
	replace d11_4 = p2_c25	if wave == 3
	replace d11_4 = e2_d23	if wave == 4
	la var d11_4 "\shortstack{Satisfied\\w MP}" 

foreach i of numlist 3 4 {
	replace d11_`i' = . 	if d11_`i' == 6 // no opinion / don't know / decline is coded as missing
	forval wv = 1/4 {
		qui count if d11_`i' > 3 & d11_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d11_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d11_`i' = (d11_`i' <= 3)	if wave == `wv' & d11_`i' != . & `num_low' < `num_high'
			replace d11_`i' = (d11_`i' < 3) 	if wave == `wv' & d11_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if satisfied; = 0 if not satisfied


**** Domain 12: Beliefs about economic effects of non-refugee immigrants // 2 outcomes
forval i = 1/2 {
	gen d12_`i' = .
}

* 1. /e_g4/e2_g4: Taking everything into consideration, would you say the overall economic effect of foreigners other than refugees on Uganda has been positive, negative, or neutral?
	replace d12_1 = e_g4 	if wave == 2
	replace d12_1 = e2_g4 	if wave == 4
	la var d12_1 "\shortstack{Immigrants:\\Effect on\\Economy}" 

* 2. /e_g5: How about the overall economic effect of foreigners other than refugees on you personally? 
	replace d12_2 = e_g5 	if wave == 2
	replace d12_2 = e2_g5 	if wave == 4
	la var d12_2 "\shortstack{Immigrants:\\Effect on\\You}" 

foreach i of numlist 1 2 {
	forval wv = 1/4 {
		qui count if d12_`i' > 3 & d12_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d12_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d12_`i' = (d12_`i' <= 3)	if wave == `wv' & d12_`i' != . & `num_low' < `num_high'
			replace d12_`i' = (d12_`i' < 3) 	if wave == `wv' & d12_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative


**** Domain 13: Social attitudes about other non-refugee immigrants // 1 outcome
gen d13_1 = .

* 1. /e_g6/e2_g6: What effect have foreigners besides refugees had on culture in Uganda? 
	replace d13_1 = e_g6 	if wave == 2
	replace d13_1 = e2_g6 	if wave == 4
	la var d13_1 "\shortstack{Immigrants:\\Effect on\\Culture}" 

forval wv = 1/4 {
		qui count if d13_1 > 3 & d13_1 != . & wave == `wv'
		local num_high = r(N)
		qui count if d13_1 < 3 & wave == `wv'
		local num_low = r(N)
			replace d13_1 = (d13_1 <= 3)	if wave == `wv' & d13_1 != . & `num_low' < `num_high'
			replace d13_1 = (d13_1 < 3) 	if wave == `wv' & d13_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if positive; = 0 if negative


**** Domain 14: Contact with Ugandans from another tribe // 8 outcomes
forval i = 1/7 {
	gen d14_`i' = .
}

* 1. /e_c2/e2_c2: How many of your customers are Ugandans from a different tribe? 
	replace d14_1 = e_c2 	if wave == 2
	replace d14_1 = e2_c2 	if wave == 4
	la var d14_1 "\shortstack{Customers\\from\\Other Tribes}" 

* 2. /e_c16/e2_c16: How many businesses in your sector in this area are managed by Ugandans from another tribe?
	replace d14_2 = e_c16 	if wave == 2
	replace d14_2 = e2_c16 	if wave == 4
	la var d14_2 "\shortstack{Business\\from\\Other Tribes}" 

foreach i of numlist 1 2 {
	forval wv = 1/4 {
		qui count if d14_`i' > 3 & d14_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d14_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d14_`i' = (d14_`i' <= 3)	if wave == `wv' & d14_`i' != . & `num_low' < `num_high'
			replace d14_`i' = (d14_`i' < 3) 	if wave == `wv' & d14_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if positive; = 0 if negative

* 3. /e_c10/e2_c10: How many of your business collaborators are Ugandans from a different tribe? 
	replace d14_3 = e_c10 	if wave == 2
	replace d14_3 = 0 		if wave == 2 & e_c7 == 2
	replace d14_3 = e2_c10 	if wave == 4
	replace d14_3 = 0 		if wave == 4 & e2_c7 == 2
	la var d14_3 "\shortstack{Business\\Collab from\\Other Tribes}" 

* 4. /e_c11/e2_c11: Would you be open to collaborating with Ugandans from other tribes? 
	replace d14_4 = 1 		if wave == 2 & e_c11 == 1
	replace d14_4 = 0 		if wave == 2 & e_c11 == 2
	replace d14_4 = 1 		if wave == 2 & e_c10 > 0 & e_c10 != .
	replace d14_4 = 1 		if wave == 4 & e2_c11 == 1
	replace d14_4 = 0 		if wave == 4 & e2_c11 == 2
	replace d14_4 = 1 		if wave == 4 & e2_c10 > 0 & e_c10 != .
	la var d14_4 "\shortstack{Open to\\Collab w\\Other Tribes}" 

* 5. /e_b7/e2_b7: Have you ever had an apprentice or person from outside your household at your business who was learning skills but not paid who was from another tribe? 
	replace d14_5 = 1 	if wave == 2 & e_b7 == 1
	replace d14_5 = 0 	if wave == 2 & e_b7 == 2
	replace d14_5 = 0 	if wave == 2 & e_b5 == 2

	replace d14_5 = 1 	if wave == 4 & e2_b7 == 1
	replace d14_5 = 0 	if wave == 4 & e2_b7 == 2
	replace d14_5 = 0 	if wave == 4 & e2_b5 == 2

	la var d14_5 "\shortstack{Apprentices\\from\\Other Tribes}" 

* 6. /e_b25/e2_b25: Are any of your employees from a different tribe than you? 
	replace d14_6 = 1 	if wave == 2 & e_b25 == 1
	replace d14_6 = 0 	if wave == 2 & e_b25 == 2
	replace d14_6 = 0 	if wave == 2 & e_b23 == 0

	replace d14_6 = 1 	if wave == 4 & e2_b25 == 1
	replace d14_6 = 0 	if wave == 4 & e2_b25 == 2
	replace d14_6 = 0 	if wave == 4 & e2_b23 == 0
	la var d14_6 "\shortstack{Employees\\from\\Other Tribes}" 

* 7. /e_e10/e2_e10: In the past 30 days, how many people from a different tribe have you contacted for any social reason, such as having a long conversation? 
	replace d14_7 = e_e10 	if wave == 2
	replace d14_7 = e2_e10 	if wave == 4
	la var d14_7 "\shortstack{Contacts\\from\\Other Tribes}" 

* 8. /BN module: Number of people from another tribe listed in the networks module.
	gen d14_8_tmp1 = 1 	if wave == 2 & (((e_bn5___1 != b_p9) & (e_bn5___1 != -44 & e_bn5___1 != -45)) | (e_bn5___1 == -44 | e_bn5___1 == -45)) & e_bn5___1 != .
	gen d14_8_tmp2 = 1 	if wave == 2 & (((e_bn5___2 != b_p9) & (e_bn5___2 != -44 & e_bn5___2 != -45)) | (e_bn5___2 == -44 | e_bn5___2 == -45)) & e_bn5___2 != .
	gen d14_8_tmp3 = 1 	if wave == 2 & (((e_bn5___3 != b_p9) & (e_bn5___3 != -44 & e_bn5___3 != -45)) | (e_bn5___3 == -44 | e_bn5___3 == -45)) & e_bn5___3 != .
	gen d14_8_tmp4 = 1 	if wave == 2 & (((e_bn5___4 != b_p9) & (e_bn5___4 != -44 & e_bn5___4 != -45)) | (e_bn5___4 == -44 | e_bn5___4 == -45)) & e_bn5___4 != .

	replace d14_8_tmp1 = 1 	if wave == 4 & (((e2_bn5_1 != b_p9) & (e2_bn5_1 != -44 & e2_bn5_1 != -45)) | (e2_bn5_1 == -44 | e2_bn5_1 == -45)) & e2_bn5_1 != .
	replace d14_8_tmp2 = 1 	if wave == 4 & (((e2_bn5_2 != b_p9) & (e2_bn5_2 != -44 & e2_bn5_2 != -45)) | (e2_bn5_2 == -44 | e2_bn5_2 == -45)) & e2_bn5_2 != .
	replace d14_8_tmp3 = 1 	if wave == 4 & (((e2_bn5_3 != b_p9) & (e2_bn5_3 != -44 & e2_bn5_3 != -45)) | (e2_bn5_3 == -44 | e2_bn5_3 == -45)) & e2_bn5_3 != .

	egen d14_8 = rowtotal(d14_8_tmp1 d14_8_tmp2 d14_8_tmp3 d14_8_tmp4) 	if (wave == 2| wave==4) & phone_survey == 0
	drop d14_8_tmp*
	la var d14_8 "\shortstack{Networks\\from\\Other Tribes}" 

**** Domain 15: Beliefs about economic effects of Ugandans from another tribe // 1 outcome
gen d15_1 = .

* 1. /e_c22/e2_c22: How do the businesses managed by Ugandans from a different tribe affect your business overall? Do they help you a lot, help you a little, hurt you a little, hurt you a lot, or have no effect on you?
	gen e_c21_tmp = e_c21
	replace e_c21_tmp = 3 	if e_c15 == 5
	replace d15_1 = 1		if e_c22 <= e_c21_tmp & e_c22 != . & e_c21_tmp !=. & wave == 2 // compare ugandan business influence w/ other tribe business influence
	replace d15_1 = 0 		if e_c22 > e_c21_tmp & e_c22 != . & e_c21_tmp !=. & wave == 2
	drop e_c21_tmp

	gen e2_c21_tmp = e2_c21
	replace e2_c21_tmp = 3 	if e2_c15 == 5
	replace d15_1 = 1		if e2_c22 <= e2_c21_tmp & e2_c22 != . & e2_c21_tmp !=. & wave == 4 // compare ugandan business influence w/ other tribe business influence
	replace d15_1 = 0 		if e2_c22 > e2_c21_tmp & e2_c22 != . & e2_c21_tmp !=. & wave == 4
	drop e2_c21_tmp

	la var d15_1 "\shortstack{Other Tribes:\\Effect on\\Your Business}" 

**** Domain 16: Social attitudes about Ugandans from another tribe // 1 outcome
gen d16_1 = .

* 1. /e_k1/e2_k1: I would be comfortable marrying a Ugandan from another tribe.
* 1. /e_k2/e2_k2: I would be comfortable having a Ugandan from another tribe marry a member of my family.
* 1. /e_k3/e2_k3: I would be comfortable having a Ugandan from another tribe as a close, personal friend.
* 1. /e_k4/e2_k4: I would be comfortable having a Ugandan from another tribe as a neighbor.
	gen d16_1_tmp1 = e_k1 	if wave == 2
	gen d16_1_tmp2 = e_k2 	if wave == 2
	gen d16_1_tmp3 = e_k3 	if wave == 2
		replace d16_1_tmp3 = 1 	if e_k1 == 1 & e_k2 == 1
	gen d16_1_tmp4 = e_k4 	if wave == 2
		replace d16_1_tmp4 = 1 	if e_k1 == 1 & e_k2 == 1

	replace d16_1_tmp1 = e2_k1 	if wave == 4
	replace d16_1_tmp2 = e2_k2 	if wave == 4
	replace d16_1_tmp3 = e2_k3 	if wave == 4
		replace d16_1_tmp3 = 1 		if e2_k1 == 1 & e2_k2 == 1
	replace d16_1_tmp4 = e2_k4 	if wave == 4
		replace d16_1_tmp4 = 1 		if e2_k1 == 1 & e2_k2 == 1

	la var d16_1_tmp1 "\shortstack{Comfortable\\Another Tribe\\Spouse}" 
	la var d16_1_tmp2 "\shortstack{Comfortable\\Another Tribe\\Spouse\\Family Member}" 
	la var d16_1_tmp3 "\shortstack{Comfortable\\Another Tribe\\Friends}" 
	la var d16_1_tmp4 "\shortstack{Comfortable\\Another Tribe\\Neighbors}" 
	
foreach i of numlist 1 2 3 4 {
	replace d16_1_tmp`i' = 3 	if d16_1_tmp`i' == 6
	forval wv = 1/4 {
		qui count if d16_1_tmp`i' > 3 & d16_1_tmp`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if d16_1_tmp`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace d16_1_tmp`i' = (d16_1_tmp`i' <= 3)	if wave == `wv' & d16_1_tmp`i' != . & `num_low' < `num_high'
			replace d16_1_tmp`i' = (d16_1_tmp`i' < 3) 	if wave == `wv' & d16_1_tmp`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if agree; = 0 if disagree*/

swindex d16_1_tmp1 d16_1_tmp2 d16_1_tmp3 d16_1_tmp4 if (wave == 2 | wave == 4), generate(d16_1) normby(control) replace
la var d16_1 "\shortstack{Other Tribes:\\Social\\Proximity}" 

**** Domain 17: Gender roles // 3 outcomes
forval i = 1/3 {
	gen d17_`i' = .
}

* 1. /e_d3/e2_d3: Do you share all of your profits from this business with your spouse? 
	replace d17_1 = 1 	if wave == 2 & e_d3 == 1
	replace d17_1 = 0 	if wave == 2 & e_d3 == 2
	replace d17_1 = 1 	if wave == 4 & e2_d3 == 1
	replace d17_1 = 0 	if wave == 4 & e2_d3 == 2
	la var d17_1 "\shortstack{Share\\Profits w\\Spouse}" 
	
	gen d17_1_female = d17_1 	if ls_b4 == 2
	la var d17_1_female "\shortstack{Share\\Profits w\\Spouse}" 	
	gen d17_1_male = d17_1 		if ls_b4 == 1	
	la var d17_1_male "\shortstack{Share\\Profits w\\Spouse}" 	

* 2. /e_d4/e2_d4: Who decides how the household's money is spent?
	replace d17_2 = 1 	if wave == 2 & (e_d4 == 1 | e_d4 == 2 | e_d4 == 5 | e_d4 == 6) & ls_b4 == 2 // women
	replace d17_2 = 0 	if wave == 2 & (e_d4 == 3 | e_d4 == 4) & ls_b4 == 2
	replace d17_2 = 1 	if wave == 4 & (e2_d4 == 1 | e2_d4 == 2 | e2_d4 == 5 | e2_d4 == 6) & ls_b4 == 2 // women
	replace d17_2 = 0 	if wave == 4 & (e2_d4 == 3 | e2_d4 == 4) & ls_b4 == 2
	la var d17_2 "\shortstack{Women\\Decide\\Expenditure}" 
	
	replace d17_3 = 1 	if wave == 2 & (e_d4 == 1 | e_d4 == 2 | e_d4 == 5 | e_d4 == 6) & ls_b4 == 1 // men
	replace d17_3 = 0 	if wave == 2 & (e_d4 == 3 | e_d4 == 4) & ls_b4 == 1
	replace d17_3 = 1 	if wave == 4 & (e2_d4 == 1 | e2_d4 == 2 | e2_d4 == 5 | e2_d4 == 6) & ls_b4 == 1 // men
	replace d17_3 = 0 	if wave == 4 & (e2_d4 == 3 | e2_d4 == 4) & ls_b4 == 1
	la var d17_3 "\shortstack{Men\\Decide\\Expenditure}" 

**** Domain 18: COVID-19 Household Shock // 5 outcomes
forval i = 1/5 {
	gen d18_`i' = .
}

* 1. m_cov7/: In total, about how much income did your family earn during the 4 months of the lockdown (April - July)? Do not count money that you borrowed. 
	replace d18_1 = m_cov7 	if wave == 1
	replace d18_1 = 0 		if d18_1 < 0 // -98 and -44
	replace d18_1 = d18_1 * 1000 // thousand UGX to UGX
	la var d18_1 "\shortstack{COVID:\\Income}" 

* 2. m_cov13/: During the lockdown, how often did you or anyone in your household go without enough food to eat? Always, often, sometimes, or never? 
	replace d18_2 = m_cov13 	if wave == 1 
	la var d18_2 "\shortstack{COVID:\\Have Food}" 

* 3. m_cov14/: During the lockdown, how often did you or anyone in your household struggle to afford basic household expenses other than food (such as medicine, rent, school fees)? 
	replace d18_3 = m_cov14 	if wave == 1
	la var d18_3 "\shortstack{COVID:\\Fine w\\Household\\Expenses}" 

foreach i of numlist 2 3 {
	forval wv = 1/2 {
		qui sum d18_`i' 	if wave == `wv', de
		if `r(N)' != 0 {
			replace d18_`i' = (d18_`i' >= 3) 	if wave == `wv' & d18_`i' != .
		}
	}
} // = 1 if sometimes or never; = 0 if always or often

* 4. m_cov15/: During the lockdown, did you or anyone in your household have to sell assets (jewelry, furniture, clothing, tools, machines, land) in order to afford basic household expenses? 
	replace d18_4 = 1 	if wave == 1 & m_cov15 == 2
	replace d18_4 = 0 	if wave == 1 & m_cov15 == 1 // = 1 if no; = 0 if yes
	la var d18_4 "\shortstack{COVID:\\No Need\\to Sell\\Assets}" 

* 5. m_cov10/: How much did you borrow during the lockdown to pay for basic necessities like food, housing, and medicine? 
	replace d18_5 = m_cov10 	if wave == 1
	replace d18_5 = 0 			if m_cov9 == 2
	replace d18_5 = d18_5 * 1000
	la var d18_5 "\shortstack{COVID:\\Borrowing}" 

**** Domain 19: Government or NGO Support // 4 outcomes
forval i = 1/5 {
	gen d19_`i' = .
}

* 1. m_cov16 / m_cov19 / e_d21/e2_pf1: During the lockdown (April - July), did anyone in your household receive any government or NGO support? / Did anyone in your household receive any government or NGO support in 2019 or 2020, before the lockdown? / Over the past year, has your household received any assistance from an NGO or international organization?/ 
	replace d19_1 = (m_cov19 == 1)	if wave == 1 & (m_cov19 == 1 | m_cov19 == 2 | m_cov19 == 3)
	replace d19_1 = 1				if wave == 1 & m_cov16 == 1 // pooling before and during lockdown for now
	replace d19_1 = (e_d21 == 1)	if wave == 2 & (e_d21 != .)
	replace d19_1 = (e2_pf1 == 1)	if wave == 4 //& (e2_pf1 != .)
	la var d19_1 "\shortstack{Reported\\Any Support$^+$}"

* 2. m_cov17b / e_d24/ e2_pf3==2: Do you remember who is running this program? Did they mention YARID?
	replace d19_2 = (m_cov20b == 1)	if wave == 1 & (m_cov19 == 1 | m_cov19 == 2 | m_cov19 == 3)
	replace d19_2 = 1				if wave == 1 & m_cov17b == 1 // pooling before and during lockdown for now
	replace d19_2 = (e_d24 == 1)	if wave == 2 & (e_d21 != .)
	replace d19_2 = (e2_pf3_2 == 1) if wave == 4 //& (e2_pf3_2 != .)
	la var d19_2 "\shortstack{Associated\\Support w\\YARID$^+$}"

* 3. m_cov17c / e_d25/ e2_pf3==3: Do you remember who is running this program? Did they mention IRC?
	replace d19_3 = (m_cov20c == 1)	if wave == 1 & (m_cov19 == 1 | m_cov19 == 2 | m_cov19 == 3)
	replace d19_3 = 1				if wave == 1 & m_cov17c == 1 // pooling before and during lockdown for now
	replace d19_3 = (e_d25 == 1)	if wave == 2 & (e_d21 != .)
	replace d19_3 = (e2_pf3_3 == 1) if wave == 4 //& (e2_pf3_3 != .)
	la var d19_3 "\shortstack{Associated\\Support w\\Data Firm$^+$}"

* 4. m_cov17a / e_d24/e2_yp6==2,3,4: Do you remember the purpose of this program? Did they mention refugees?
	replace d19_4 = (m_cov20a == 1)	if wave == 1 & (m_cov19 == 1 | m_cov19 == 2 | m_cov19 == 3)
	replace d19_4 = 1				if wave == 1 & m_cov17a == 1 // pooling before and during lockdown for now
	replace d19_4 = (e_d27 == 1)	if wave == 2 & (e_d21 != .)
	replace d19_4 = (e2_pf4 == 1)	if wave == 4 & (e2_pf1 != .)
	replace d19_4 = 1 				if wave == 4 & e2_pf6 == 1
	replace d19_4 = 1				if wave == 4 & e2_yp6_2 == 1 & treatment != 4 // For people who mentioned the program was delivered by YARID, but not including info as "support"
	replace d19_4 = 1 				if wave == 4 & e2_yp6_3 == 1 & treatment != 4 // For people who mentioned the program was delivered by YARID, but not including info as "support"
	replace d19_4 = 1 				if wave == 4 & e2_yp6_4 == 1 & treatment != 4 // For people who mentioned the program was delivered by YARID, but not including info as "support"
	replace d19_4 = 1 				if wave == 4 & e2_yp7 == 1 & treatment != 4 // For people who mentioned the program was delivered by YARID, but not including info as "support"
	la var d19_4 "\shortstack{Associated\\Support w\\Refugees$^+$}"

**** Domain 20: Importance of Tribe // 1 outcomes
* 1. /e_k17/e2_k17: How important would you say your tribe is to you? Very important, somewhat important, neutral, not very important, or not at all important?
	gen d20_1 = e_k17  if wave == 2
	replace d20_1 = e2_k17 if wave == 4
	replace d20_1 = (d20_1 < 3) 	if (wave == 2 | wave == 4) & d20_1 != . 	// very important and somewhat important are 1 and majority
	la var d20_1 "\shortstack{Importance\\of Own\\Tribe}"


******* Domain 21 : child labor attitude (miscellaneous test 3))
* child labor attidtudes (only in endline2)
* 1. e2_p5: I would not allow anyone under age 15, including my child, to work at my business.
	gen d21_1 = e2_p5
	la var d21_1 "\shortstack{No Child\\Labor\\Under 15$^+$}"
	
* 2. e2_p6: It's okay for a child under the age of 17 to work outside of the home if their family needs the income.	
	gen d21_2 = e2_p6

foreach i of numlist 1 2 {
		qui count if d21_`i' > 3 & d21_`i' != .
		local num_high = r(N)
		qui count if d21_`i' < 3
		local num_low = r(N)
			replace d21_`i' = (d21_`i' <= 3)	if d21_`i' != . & `num_low' < `num_high'
			replace d21_`i' = (d21_`i' < 3) 	if d21_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if agree; = 0 if disagree

gen d21_2_tmp = .
	replace d21_2_tmp = 1 	if d21_2 == 0
	replace d21_2_tmp = 0 	if d21_2 == 1
	drop d21_2
rename d21_2_tmp d21_2
// 1 agree (disagree on child labor)
	la var d21_2 "\shortstack{No Child\\Labor\\Under 17$^+$}"
	
gen treatment_childlabor = .
	replace treatment_childlabor = 6 	if treatment == 1 | treatment == 2 | treatment == 5 | treatment == 6
	replace treatment_childlabor = 3 	if treatment == 3
	replace treatment_childlabor = 4 	if treatment == 4	
	
**** Domain Misc (Endline2)
forval i = 1/8 {
	gen misc_`i' = .
}

* 1. e2_f20: Compared to the average refugee in Kampala, how would you describe the economic situation of your household? Much better, somewhat better, about the same, somewhat worse, or much worse?
	replace misc_1 = e2_f20 	if wave == 4
	la var misc_1 "\shortstack{Econ Cond\\Better Than\\Refugees}"	
	
* 2. e2_d24: In the past month, how much of the time were you a happy person?
	replace misc_2 = e2_d24 	if wave == 4
	la var misc_2 "\shortstack{Most Time\\Last Month\\Happy}"	
	
* 3. e2_d25: In the past month, how much of the time did you feel calm and peaceful?
	replace misc_3 = e2_d25 	if wave == 4
	la var misc_3 "\shortstack{Most Time\\Last Month\\Calm}"	
	
* 4. e2_d26: In the past month, how much of the time did you feel down-hearted and sad?
	replace misc_4 = e2_d26 	if wave == 4
	la var misc_4 "\shortstack{Most Time\\Last Month\\Sad}"	
	
* 5. e2_f0: Are all foreigners in Uganda refugees?
	replace misc_5 = 1 	if e2_f0 == 2 & wave == 4
	replace misc_5 = 0 	if (e2_f0 == 1 | e2_f0 == .) & wave == 4
	la var misc_5 "\shortstack{Not All\\Foreigners\\Are Refugees}"	
	
* 6. e2_f11: Are refugees allowed to vote?
	replace misc_6 = 1 	if e2_f11 == 1 & wave == 4
	replace misc_6 = 0 	if (e2_f11 == 2 | e2_f11 == .) & wave == 4	
	la var misc_6 "\shortstack{Refugees\\Allowed to\\Vote}"	

* 7. e2_f13: Thinking about the refugees that come to Uganda, how much control do you think that Uganda has over the immigrants who are able to enter the country? (almost complete control, a moderate degree of control, no control).
	replace misc_7 = 1 	if (e2_f13 == 2 | e2_f13 == 3) & wave == 4
	replace misc_7 = 0 	if (e2_f13 == 1 | e2_f13 == .) & wave == 4
	la var misc_7 "\shortstack{Much Control\\Over Immigrant\\Entry}"	

* 8. e2_f21: Are there any community or nonprofit organizations led by refugees?
	replace misc_8 = 1 	if e2_f21 == 1 & wave == 4
	replace misc_8 = 0 	if (e2_f21 == 2 | e2_f21 == .) & wave == 4
	la var misc_8 "\shortstack{Exist NGOs\\Led By\\Refugees}"	
	
foreach i of numlist 1/4 {
	replace misc_`i' = 3 	if misc_`i' == 6
	forval wv = 1/4 {
		qui count if misc_`i' > 3 & misc_`i' != . & wave == `wv'
		local num_high = r(N)
		qui count if misc_`i' < 3 & wave == `wv'
		local num_low = r(N)
			replace misc_`i' = (misc_`i' <= 3)	if wave == `wv' & misc_`i' != . & `num_low' < `num_high'
			replace misc_`i' = (misc_`i' < 3) 	if wave == `wv' & misc_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	}
} // = 1 if agree; = 0 if disagree
	
*==== Monetary measures: USD ====*
* CPI taken from https://nso-uganda.opendataforafrica.org/uijrkhd/consumer-price-index; 
* November 2019 figure comes from Headline numbers in https://www.ubos.org/wp-content/uploads/publications/07_2021CPI_PUBLICATION_FOR_JULY_2021.pdf
* Exchange rate taken from https://www.investing.com/currencies/usd-ugx-historical-data
* Both accessed April 17, 2023
foreach var of varlist d2_1 d9_2 d9_5 d10_1 d10_3 d18_1 d18_5 {
	replace `var' = (`var' / 3695) /(111.81/108.52)  	if wave == 1 // Oct 2020/Nov 2019
	replace `var' = (`var' / 3695) /(112.67/108.52) 	if wave == 2 // May 2021/Nov 2019
	replace `var' = (`var' / 3695) /(113.06/108.52)		if wave == 3 // Aug 2021/Nov 2019
	replace `var' = (`var' / 3695) /(116.38/108.52)		if wave == 4 // Mar 2022/Nov 2019
	gen `var'_stat = .
	forval wv = 1/4 {
		winsor2 `var' if wave == `wv', replace cuts(1 99)
		replace `var'_stat = `var' 		if wave == `wv' // for statistics	
		replace `var' = asinh(`var') 	if wave == `wv'
	}
}

end
MidlineEndlinePhone2Endline2


*++++ Gen baseline variables ++++*
capture program drop Baseline
program Baseline

*==== Gen 3pt Likert questions (1 = positive, 2 = no effect, 3 = negative) ====*

foreach var of varlist b_g12 b_g13 b_g14 b_g17 b_g18 b_g19 {
	gen `var'_tmp = .
	replace `var'_tmp = 1 	if `var' == 1
	replace `var'_tmp = 2 	if `var' == 3
	replace `var'_tmp = 3 	if `var' == 2	
} 

foreach var of varlist b_g15 b_g16 b_g20 b_g21 {
	gen `var'_tmp = .
	replace `var'_tmp = 1 	if `var' == 2
	replace `var'_tmp = 2 	if `var' == 3
	replace `var'_tmp = 3 	if `var' == 1	
}

*==== Gen baseline outcome variables ====* 
**** Domain 1: Support for inclusive refugee hosting // 8 + 3 outcomes
forval i = 1/11 {
	gen bd1_`i' = .
}

* 1. b_j1: Overall, during coronavirus, I am in favor of Uganda hosting and assisting refugees.
	replace bd1_1 = b_j1
	
	gen bd1_1_strongagree = (bd1_1 == 1) 	if bd1_1 != .
	gen bd1_1_strongdisagree = (bd1_1 == 5) if bd1_1 != .
	
* 2. b_j1: After coronavirus, I am in favor of Uganda hosting and assisting refugees.
	replace bd1_2 = b_j1

* 3. : In July refugees from Congo were allowed to come to Uganda. They were tested for coronavirus, quarantined, and settled into camps. I am in favor of allowing refugees who test negative to move to Uganda right now.
	replace bd1_3 = b_j6

	gen bd1_3_strongagree = (bd1_3 == 1) 	if bd1_3 != .
	gen bd1_3_strongdisagree = (bd1_3 == 5) if bd1_3 != .
	
* 4. b_j6: After coronavirus ends, Uganda should accept more refugees.
	replace bd1_4 = b_j6

* 5. b_j2: During coronavirus, Uganda should relocate all refugees to live in the settlements, including those currently living in Kampala.
	replace bd1_5 = b_j2

	gen bd1_5_strongagree = (bd1_5 == 5) 	if bd1_5 != .
	gen bd1_5_strongdisagree = (bd1_5 == 1) if bd1_5 != .
	
* 6. : For those who answered “agree” or “strongly agree”: Should the relocation be permanent or only during coronavirus?
	replace bd1_6 = b_j2

* 7. b_j3: Uganda should continue allowing refugees who already live in Uganda to work outside the settlements, according to any lockdown rules, during coronavirus.
	replace bd1_7 = b_j3

	gen bd1_7_strongagree = (bd1_7 == 1) 	if bd1_7 != .
	gen bd1_7_strongdisagree = (bd1_7 == 5) if bd1_7 != .
	
* 8. b_j3: After coronavirus ends, Uganda should continue allowing refugees to work outside the settlements.
	replace bd1_8 = b_j3

* 9. / e_j4: Uganda should continue to provide land to refugees in the settlements.
	replace bd1_9 = b_j4

* 10. / e_j7: For refugees who are already in Uganda, which option do you think Uganda should follow?
	replace bd1_10 = (b_j7 <= 2) if (b_j7 != .)

* 11. / e_j5: Uganda should allow refugees to become full citizens if they have lived in Uganda for a long time and would like to become a Ugandan. As citizens, they would have the right to vote in Ugandan elections.
	replace bd1_11 = b_j5

foreach i of numlist 1/9 11 {
	gen missing_bd1_`i' = bd1_`i' == .
	replace bd1_`i' = 3 	if bd1_`i' == 6
		qui count if bd1_`i' > 3 & bd1_`i' != .
		local num_high = r(N)
		qui count if bd1_`i' < 3
		local num_low = r(N)
			replace bd1_`i' = (bd1_`i' <= 3)	if bd1_`i' != . & `num_low' < `num_high'
			replace bd1_`i' = (bd1_`i' < 3) 	if bd1_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd1_`i', de
	replace bd1_`i' = `r(mean)' 		if bd1_`i' == .
} // = 1 if agree; = 0 if disagree

gen missing_bd1_10 = (bd1_10 == .)

gen bd1_5_tmp = 1 - bd1_5
drop bd1_5
rename bd1_5_tmp bd1_5 // disagree w relocation means support
replace bd1_6 = bd1_5

rename (*bd1_9 *bd1_10 *bd1_11) (*bd1a_9 *bd1a_10 *bd1a_11) // Not included in PAP


**** Domain 2: Business profits
gen bd2_1 = .

* 1. b_b38: What were the profits of your business during the last 30 days?
	replace bd2_1 = b_b38 // no range update
	gen missing_bd2_1 = bd2_1 == .
	winsor2 bd2_1, replace cuts(1 99)
	qui sum bd2_1, de
	replace bd2_1 = `r(mean)' 	if bd2_1 == . // 6 obs missing

	
**** Domain 3: Knowledge of refugees and hosting policy // 3 outcomes
forval i = 1/3 {
	gen bd3_`i' = .
}

* 1. b_f13: How many refugees in Uganda live outside of camps or settlements: all, most, some, few, or none? (“Some” or “few” will be considered correct answers)
	replace bd3_1 = 1 	if b_f13 == 3 | b_f13 == 4
	replace bd3_1 = 0 	if b_f13 == 1 | b_f13 == 2 | b_f13 == 5 | b_f13 == .

* 2. b_f14: Are refugees allowed to live outside of the camps or settlements? (“yes” is correct)
	replace bd3_2 = 1 	if b_f14 == 1
	replace bd3_2 = 0 	if b_f14 == 2 | b_f14 == .

* 3. b_f15: Are any of the international donations to refugees in Uganda shared with Ugandans? (“yes” is correct)
	replace bd3_3 = 1 	if b_f15 == 1
	replace bd3_3 = 0 	if b_f15 == 2 | b_f15 == .

foreach i of numlist 1 2 3 {
	gen missing_bd3_`i' = bd3_`i' == .
	qui sum bd3_`i', de
	replace bd3_`i' = `r(mean)' 		if bd3_`i' == .
} // = 1 if agree; = 0 if disagree


**** Domain 4: Beliefs about economic effects of refugees // 4 outcomes
forval i = 1/4 {
	gen bd4_`i' = .
}

* 1. b_c23: How do the [sector] businesses managed by people from other countries affect your business overall? Do they help you a lot, help you a little, hurt you a little, hurt you a lot, or have no effect on you?
	gen b_c21_tmp = b_c21
	replace b_c21_tmp = 3 	if b_c15 == 5
	replace bd4_1 = 1		if b_c23 <= b_c21_tmp & b_c23 != . & b_c21_tmp !=. // compare ugandan business influence w/ other country business influence
	replace bd4_1 = 0 		if b_c23 > b_c21_tmp & b_c23 != . & b_c21_tmp !=.
	drop b_c21_tmp
	gen missing_bd4_1 = bd4_1 == .
	qui sum bd4_1, de
	replace bd4_1 = `r(mean)' 	if bd4_1 == .

* 2. b_g1: Taking everything into consideration, would you say the overall economic effect of refugees on Uganda has been positive, negative, or neutral?
	replace bd4_2 = b_g1

* 3. b_g2: How about the overall economic effect of refugees on you personally?
	replace bd4_3 = b_g2

* 4. b_f20: How many refugees have skills and contribute to the economy?
	replace bd4_4 = b_f20

foreach i of numlist 2 3 4 {
	gen missing_bd4_`i' = bd4_`i' == .
		qui count if bd4_`i' > 3 & bd4_`i' != .
		local num_high = r(N)
		qui count if bd4_`i' < 3
		local num_low = r(N)
			replace bd4_`i' = (bd4_`i' <= 3)	if bd4_`i' != . & `num_low' < `num_high'
			replace bd4_`i' = (bd4_`i' < 3) 	if bd4_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd4_`i', de
	replace bd4_`i' = `r(mean)' 		if bd4_`i' == .
} // = 1 if positive; = 0 if negative


**** Domain 4a: Beliefs about economic effects of Congolese refugees // 4 outcomes
forval i = 1/4 {
	gen bd4a_`i' = .
}

* 1. b_g7: Taking everything into consideration, would you say the overall economic effect of Congolese on Uganda has been positive, negative, or neutral?
	replace bd4a_1 = b_g7
	gen missing_bd4a_1 = bd4a_1 == .
		qui count if bd4a_1 > 3 & bd4a_1 != .
		local num_high = r(N)
		qui count if bd4a_1 < 3
		local num_low = r(N)
			replace bd4a_1 = (bd4a_1 <= 3)	if bd4a_1 != . & `num_low' < `num_high'
			replace bd4a_1 = (bd4a_1 < 3) 	if bd4a_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd4a_1, de
	replace bd4a_1 = `r(mean)' 	if bd4a_1 == .

* 2. b_g19: How have access and quality of schools and health facilities been affected by Congolese in Kampala?
	replace bd4a_2 = b_g19_tmp

* 3. b_g20: How have rents been affected by Congolese in Kampala?
	replace bd4a_3 = b_g20_tmp

* 4. b_g21: How have prices of goods you buy, other than rents, been affected by Congolese in Kampala?
	replace bd4a_4 = b_g21_tmp

foreach i of numlist 2 3 4 {
	gen missing_bd4a_`i' = bd4a_`i' == .
		qui count if bd4a_`i' > 2 & bd4a_`i' != .
		local num_high = r(N)
		qui count if bd4a_`i' < 2
		local num_low = r(N)
			replace bd4a_`i' = (bd4a_`i' <= 2)	if bd4a_`i' != . & `num_low' < `num_high'
			replace bd4a_`i' = (bd4a_`i' < 2) 	if bd4a_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd4a_`i', de
	replace bd4a_`i' = `r(mean)' 		if bd4a_`i' == .
}


**** Domain 4b: Beliefs about economic effects of Somali refugees // 4 outcomes
forval i = 1/4 {
	gen bd4b_`i' = .
}

* 1. b_g9: Taking everything into consideration, would you say the overall economic effect of Somalis on Uganda has been positive, negative, or neutral?
	replace bd4b_1 = b_g9
	gen missing_bd4b_1 = bd4b_1 == .
		qui count if bd4b_1 > 3 & bd4b_1 != .
		local num_high = r(N)
		qui count if bd4b_1 < 3
		local num_low = r(N)
			replace bd4b_1 = (bd4b_1 <= 3)	if bd4b_1 != . & `num_low' < `num_high'
			replace bd4b_1 = (bd4b_1 < 3) 	if bd4b_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd4b_1, de
	replace bd4b_1 = `r(mean)' 	if bd4b_1 == .

* 2. b_g14: How have access and quality of schools and health facilities been affected by Somalis in Kampala?
	replace bd4b_2 = b_g14_tmp

* 3. b_g15: How have rents been affected by Somalis in Kampala?
	replace bd4b_3 = b_g15_tmp

* 4. b_g16: How have prices of goods you buy, other than rents, been affected by Somalis in Kampala?
	replace bd4b_4 = b_g16_tmp

foreach i of numlist 2 3 4 {
	gen missing_bd4b_`i' = bd4b_`i' == .
		qui count if bd4b_`i' > 2 & bd4b_`i' != .
		local num_high = r(N)
		qui count if bd4b_`i' < 2
		local num_low = r(N)
			replace bd4b_`i' = (bd4b_`i' <= 2)	if bd4b_`i' != . & `num_low' < `num_high'
			replace bd4b_`i' = (bd4b_`i' < 2) 	if bd4b_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd4b_`i', de
	replace bd4b_`i' = `r(mean)' 		if bd4b_`i' == .
}

**** Domain 5a: Beliefs that refugees receive too much aid // 2 outcomes
forval i = 1/2 {
	gen bd5a_`i' = .
}

* 1. b_f16: How many refugees have a lot of money? All, most, some, few, or none?
	replace bd5a_1 = b_f16

* 2. b_f18: How many refugees get more assistance than they need?
	replace bd5a_2 = b_f18

**** Domain 5b: Beliefs that refugees can support themselves // 1 outcome
gen bd5b_1 = .

* 1. b_f19: How many refugees are able to support themselves financially without assistance?
	replace bd5b_1 = b_f19

foreach var of varlist bd5a_1 bd5a_2 bd5b_1 {
	gen missing_`var' = `var' == .
		qui count if `var' > 3 & `var' != .
		local num_high = r(N)
		qui count if `var' < 3
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum `var', de
	replace `var' = `r(mean)' 		if `var' == .
} // = 1 if positive; = 0 if negative


**** Domain 6: Social attitudes about refugees // 7 outcomes
forval i = 1/7 {
	gen bd6_`i' = .
}

* 1. b_g3: What effect have refugees had on culture in Uganda?
	replace bd6_1 = b_g3

* 2. b_k5: I would be comfortable marrying a refugee.
* 2. b_k6: I would be comfortable having a refugee marry a member of my family.
* 2. b_k7: I would be comfortable having a refugee as a close, personal friend.
* 2. b_k8: I would be comfortable having a refugee as a neighbor.
	gen bd6_2_tmp1 = b_k5
	gen bd6_2_tmp2 = b_k6
	gen bd6_2_tmp3 = b_k7
		replace bd6_2_tmp3 = 1 	if b_k5 == 1 & b_k6 == 1
	gen bd6_2_tmp4 = b_k8
		replace bd6_2_tmp4 = 1 	if b_k5 == 1 & b_k6 == 1

foreach i of numlist 1 2 3 4 {
	replace bd6_2_tmp`i' = 3 	if bd6_2_tmp`i' == 6	
		qui count if bd6_2_tmp`i' > 3 & bd6_2_tmp`i' != .
		local num_high = r(N)
		qui count if bd6_2_tmp`i' < 3
		local num_low = r(N)
			replace bd6_2_tmp`i' = (bd6_2_tmp`i' <= 3)	if bd6_2_tmp`i' != . & `num_low' < `num_high'
			replace bd6_2_tmp`i' = (bd6_2_tmp`i' < 3) 	if bd6_2_tmp`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if positive; = 0 if negative

swindex bd6_2_tmp1 bd6_2_tmp2 bd6_2_tmp3 bd6_2_tmp4, generate(bd6_2) normby(control) replace

gen missing_bd6_2 = bd6_2 == .

foreach var of varlist bd6_2_tmp* {
	gen missing_`var' = `var' == .
}

* 3. b_f17: How many refugees deserve sympathy and support?
	replace bd6_3 = b_f17

foreach i of numlist 1 3 {
	gen missing_bd6_`i' = bd6_`i' == .
		qui count if bd6_`i' > 3 & bd6_`i' != .
		local num_high = r(N)
		qui count if bd6_`i' < 3
		local num_low = r(N)
			replace bd6_`i' = (bd6_`i' <= 3)	if bd6_`i' != . & `num_low' < `num_high'
			replace bd6_`i' = (bd6_`i' < 3) 	if bd6_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd6_`i', de
	replace bd6_`i' = `r(mean)' 		if bd6_`i' == .
} // = 1 if positive; = 0 if negative

* 4. b_n1: Our research team has an extra UGX available. We can give it to you or share it between you and two charity organizations in Uganda. The first charity helps poor Ugandans living in Kampala. The second charity  helps refugees living in Kampala. We are going to let you decide how to split the money. How much of the UGX should we give to the charity supporting poor Ugandans in Kampala?
* 5. b_n2: How much of the remaining UGX should we give to the charity supporting refugees in Kampala?

	replace bd6_4 = b_n2 / 3000
	gen missing_bd6_4 = bd6_4 == .

	replace bd6_5 = b_n1 / 3000
	gen missing_bd6_5 = bd6_5 == .

	gen bd6c_4 = (b_n2 > b_n1) if b_n1 != .
	gen missing_bd6c_4 = (b_n1 == .)

	gen bd6c_5 = (b_n2 >= b_n1) if b_n1 != .
	gen missing_bd6c_5 = (b_n1 == .)

* 6. b_e20: How safe do you feel walking around areas in Kampala where people from other countries live? You can say very safe, somewhat safe, neutral, somewhat unsafe, very unsafe, or that it depends on the nationality.
	gen b_e20_tmp = b_e20
	replace b_e20_tmp = 5 	if b_e20 == 6 & (b_e21_2 == 1 | b_e21_4 == 1)
	replace b_e20_tmp = 3 	if b_e20 == 6 & b_e21_2 != 1 & b_e21_4 != 1

	replace bd6_6 = 1 		if b_e20_tmp <= b_e19 & b_e19 != . & b_e20_tmp !=. // compare safety around foreigners w/ safety in general
	replace bd6_6 = 0 		if b_e20_tmp > b_e19 & b_e19 != . & b_e20_tmp !=.
	drop b_e20_tmp

	gen missing_bd6_6 = bd6_6 == .

* 7. b_e23: Is there tension between Ugandans and people from other nationalities?
	replace bd6_7 = 1 	if b_e23 == 2
	replace bd6_7 = 0 	if b_e23 == 1

	gen missing_bd6_7 = bd6_7 == .

	
**** Domain 6a: Social attitudes about Congolese refugees // 4 outcomes
forval i = 1/4 {
	gen bd6a_`i' = .
}

* 1. b_k9: I would be comfortable marrying a Congolese.
* 1. b_k10: I would be comfortable having a Congolese marry a member of my family.
* 1. b_k11: I would be comfortable having a Congolese as a close, personal friend.
* 1. b_k12: I would be comfortable having a Congolese as a neighbor.
	gen bd6a_1_tmp1 = b_k9
	gen bd6a_1_tmp2 = b_k10
	gen bd6a_1_tmp3 = b_k11
		replace bd6a_1_tmp3 = 1 	if b_k9 == 1 & b_k10 == 1
	gen bd6a_1_tmp4 = b_k12
		replace bd6a_1_tmp4 = 1 	if b_k9 == 1 & b_k10 == 1

* 2. b_g8: What effect have Congolese had on culture in Uganda?
	replace bd6a_2 = b_g8

* 3. b_g17: Please tell us how the dress code has been affected by Congolese in Kampala. You can answer positive, negative, or no effect.
	replace bd6a_3 = b_g17_tmp

* 4. b_g18: How have acceptable behaviors (such as how people talk to each other) been affected by Congolese in Kampala?
	replace bd6a_4 = b_g18_tmp

	
**** Domain 6b: Social attitudes about Somali refugees // 4 outcomes
forval i = 1/4 {
	gen bd6b_`i' = .
}

* 1. b_k13: I would be comfortable marrying a Somalis.
* 1. b_k14: I would be comfortable having a Somalis marry a member of my family.
* 1. b_k15: I would be comfortable having a Somalis as a close, personal friend.
* 1. b_k16: I would be comfortable having a Somalis as a neighbor.
	gen bd6b_1_tmp1 = b_k13
	gen bd6b_1_tmp2 = b_k14
	gen bd6b_1_tmp3 = b_k15
		replace bd6b_1_tmp3 = 1 	if b_k13 == 1 & b_k14 == 1
	gen bd6b_1_tmp4 = b_k16
		replace bd6b_1_tmp4 = 1 	if b_k13 == 1 & b_k14 == 1

* 2. b_g10: What effect have Somalis had on culture in Uganda?
	replace bd6b_2 = b_g10

* 3. b_g12: Please tell us how the dress code has been affected by Somalis in Kampala. You can answer positive, negative, or no effect.
	replace bd6b_3 = b_g12_tmp

* 4. b_g13: How have acceptable behaviors (such as how people talk to each other) been affected by Somalis in Kampala?
	replace bd6b_4 = b_g13_tmp

foreach var of varlist bd6a_1_tmp1 bd6a_1_tmp2 bd6a_1_tmp3 bd6a_1_tmp4 bd6b_1_tmp1 bd6b_1_tmp2 bd6b_1_tmp3 bd6b_1_tmp4 {
		qui count if `var' > 3 & `var' != .
		local num_high = r(N)
		qui count if `var' < 3
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if positive; = 0 if negative

swindex bd6a_1_tmp1 bd6a_1_tmp2 bd6a_1_tmp3 bd6a_1_tmp4, generate(bd6a_1) normby(control) replace
gen missing_bd6a_1 = bd6a_1 == .

swindex bd6b_1_tmp1 bd6b_1_tmp2 bd6b_1_tmp3 bd6b_1_tmp4, generate(bd6b_1) normby(control) replace
gen missing_bd6b_1 = bd6b_1 == .

foreach var of varlist bd6a_1_tmp* bd6b_1_tmp* {
	gen missing_`var' = `var' == .
}

foreach var of varlist bd6a_2 bd6b_2 {
	gen missing_`var' = `var' == .
		qui count if `var' > 3 & `var' != .
		local num_high = r(N)
		qui count if `var' < 3
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum `var', de
	replace `var' = `r(mean)' 		if `var' == .
} // = 1 if positive; = 0 if negative

foreach var of varlist bd6a_3 bd6a_4 bd6b_3 bd6b_4 {
	gen missing_`var' = `var' == .
		qui count if `var' > 2 & `var' != .
		local num_high = r(N)
		qui count if `var' < 2
		local num_low = r(N)
			replace `var' = (`var' <= 2)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 2) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum `var', de
	replace `var' = `r(mean)' 		if `var' == .
}


**** Domain 7: Contact with refugees by choice // 7 outcomes
forval i = 1/6 {
	gen bd7_`i' = .
}

* 1. b_c12: How many of your business collaborators are from another country?
	replace bd7_1 = 0 		if b_c7 == 2
	replace bd7_1 = b_c12 	if b_c7 == 1
	gen missing_bd7_1 = bd7_1 == .
	qui sum bd7_1 if b_c7 == 1, de
	replace bd7_1 = `r(mean)' 	if b_c7 == 1 & bd7_1 == .

* 2. b_c14: Would you be open to collaborating with business owners from another country?
	replace bd7_2 = 1 	if b_c12 > 0 & b_c12 != .
	replace bd7_2 = 1 	if b_c14 == 1
	replace bd7_2 = 0 	if b_c14 == 2

* 3. b_c5: In the last 30 days, have you bought supplies (such as materials for your business), tools, or machines from someone from another country?
	replace bd7_3 = (b_c5 == 1)

* 4. b_b16: Have you ever had an apprentice or person from outside your household at your business who was learning skills but not paid who was from another country?
	replace bd7_4 = 1 	if b_b16 == 1
	replace bd7_4 = 0 	if b_b16 == 2
	replace bd7_4 = 0 	if b_b13 == 2

* 5. b_b34: Are any of your employees from a different country than you?
	replace bd7_5 = 1 	if b_b34 == 1
	replace bd7_5 = 0 	if b_b34 == 2
	replace bd7_5 = 0 	if b_b31 == 0

* 6. b_e14: In the past 30 days, how many people from another country have you contacted for any social reason, such as having a long conversation?
	replace bd7_6 = b_e14

* 7. BN module: Number of people from another country listed in the networks module.
	gen bd7_7_tmp1 = 1 	if b_bn4___1 != 1 & b_bn4___1 != .
	gen bd7_7_tmp2 = 1 	if b_bn4___2 != 1 & b_bn4___2 != .
	gen bd7_7_tmp3 = 1 	if b_bn4___3 != 1 & b_bn4___3 != .
	gen bd7_7_tmp4 = 1 	if b_bn4___4 != 1 & b_bn4___4 != .
	egen bd7_7 = rowtotal(bd7_7_tmp1 bd7_7_tmp2 bd7_7_tmp3 bd7_7_tmp4)
	drop bd7_7_tmp*

foreach i of numlist 2 3 4 5 6 7 {
	gen missing_bd7_`i' = bd7_`i' == .
	qui sum bd7_`i', de
	replace bd7_`i' = `r(mean)' 	if bd7_`i' == .
}


**** Domain 8: Contact with refugees by circumstance / 3 outcomes
forval i = 1/3 {
	gen bd8_`i' = .
}

* 1. b_e12: How many people from other countries live in your neighborhood? Many, some, few, or none?
	replace bd8_1 = 1 	if b_e12 == 1 | b_e12 == 2
	replace bd8_1 = 0	if b_e12 == 3 | b_e12 == 4
	gen missing_bd8_1 = bd8_1 == .

* 2. b_c17: How many businesses in your sector in this area are managed by people from another country?
	replace bd8_2 = b_c17

* 3. b_c3: How many of your customers are from another country?
	replace bd8_3 = b_c3

foreach i of numlist 2 3 {
	gen missing_bd8_`i' = bd8_`i' == .
		qui count if bd8_`i' > 3 & bd8_`i' != .
		local num_high = r(N)
		qui count if bd8_`i' < 3
		local num_low = r(N)
			replace bd8_`i' = (bd8_`i' <= 3)	if bd8_`i' != . & `num_low' < `num_high'
			replace bd8_`i' = (bd8_`i' < 3) 	if bd8_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd8_`i', de
	replace bd8_`i' = `r(mean)' 		if bd8_`i' == .
}


**** Domain 9: Business practices // 10 outcomes
forval i = 1/10 {
	gen bd9_`i' = .
}

* 1. b_c24: Is your business officially registered? 
	replace bd9_1 = 1 	if b_c24 == 1
	replace bd9_1 = 0 	if b_c24 == 2
	gen missing_bd9_1 = bd9_1 == .

* 2. b_b23: If you were to sell all the business-related equipment you own right now (such as chairs, machines and tools), how much do you think you could make?
* 2. b_b25: If you were to sell all the inventory you own right now (e.g. fabric, thread, soap), how much do you think you could make?
	gen bd9_2_tmp1 = b_b23
	replace bd9_2_tmp1 = 375000 	if b_b24 == 2
	replace bd9_2_tmp1 = 875000 	if b_b24 == 4
	replace bd9_2_tmp1 = 1750000 	if b_b24 == 6
	replace bd9_2_tmp1 = 2500000 	if b_b24 == 7

	gen bd9_2_tmp2 = b_b25
	replace bd9_2_tmp2 = 125000 	if b_b26 == 1
	replace bd9_2_tmp2 = 125000 	if b_b26 == 3
	replace bd9_2_tmp2 = 125000 	if b_b26 == 5
	replace bd9_2_tmp2 = 125000 	if b_b26 == 8

	gen missing_bd9_2 = bd9_2_tmp1 == . | bd9_2_tmp2 == .

foreach i of numlist 1 2 {
	winsor2 bd9_2_tmp`i', replace cuts(1 99)
	qui sum bd9_2_tmp`i', de
	replace bd9_2_tmp`i' = `r(mean)' 		if bd9_2_tmp`i' == .	
}

replace bd9_2 = bd9_2_tmp1 + bd9_2_tmp2

* 3. b_b27: Over the past 7 days, how many hours did you work at this business?
	replace bd9_3 = b_b27

	winsor2 bd9_3, replace cuts(1 99)
	gen bd9_3_stat = bd9_3 	// for statistics	
	replace bd9_3 = asinh(bd9_3) 

* 4. b_b45: In the past year, how many times did you take out a loan for your business?
	replace bd9_4 = b_b45
	replace bd9_4 = . 	if b_b45 == -999
	replace bd9_4 = 1 	if b_b45 > 100

foreach i of numlist 3 4 {
	gen missing_bd9_`i' = bd9_`i' == .
	qui sum bd9_`i', de
	replace bd9_`i' = `r(mean)' 		if bd9_`i' == .
}

* 5. b_b48: How much total business-related debt do you currently have?
	replace bd9_5 = 0 		if b_b47 == 2
	replace bd9_5 = b_b48 	if b_b47 == 1
	gen missing_bd9_5 = bd9_5 == .
	winsor2 bd9_5 if b_b47 == 1, replace cuts(1 99)
	qui sum bd9_5 if b_b47 == 1, de
	replace bd9_5 = `r(mean)' 	if bd9_5 == .

* 6. BN module: Number of contacts listed in the networks module.
	destring b_bn_repeat_count, replace
	replace bd9_6 = b_bn_repeat_count
	gen missing_bd9_6 = bd9_6 == .

* 7. b_b41: Over the past year, how often did you spend money advertising your business? Every day, every week, every month, a couple times, or never?
	replace bd9_7 = b_b41

* 8. b_b42: How often did you keep written books/accounting records? Always, frequently, sometimes, occasionally, or never?
	replace bd9_8 = b_b42

* 9. b_b43: How often did you sell goods or provide services to customers on credit? For all sales, most sales, some sales, a few sales, or never? 
	replace bd9_9 = b_b43

* 10. b_b44: How often did you buy materials, tools, or machines for your business on credit? For all sales, most sales, some sales, a few sales, or never? 
	replace bd9_10 = b_b44

foreach i of numlist 7 8 9 10 {
	gen missing_bd9_`i' = bd9_`i' == .
		qui count if bd9_`i' > 3 & bd9_`i' != .
		local num_high = r(N)
		qui count if bd9_`i' < 3
		local num_low = r(N)
			replace bd9_`i' = (bd9_`i' <= 3)	if bd9_`i' != . & `num_low' < `num_high'
			replace bd9_`i' = (bd9_`i' < 3) 	if bd9_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd9_`i', de
	replace bd9_`i' = `r(mean)' 		if bd9_`i' == .
}

**** Domain 10: Household Well-Being // 8 outcomes
forval i = 1/8 {
	gen bd10_`i' = .
}

* 1. b_b38: What were the profits of your business during the last 30 days? 
* 1. b_d5: What were the profits of [any other household-owned] businesses (excluding this one) during the last 30 days? 
* 1. b_d8: How much wage income did you earn in the last 30 days? 
* 1. b_d11: How much wage income did [other members of your household] earn in the last 30 days? 

	gen bd10_1_tmp1 = b_b38 // no range update

	gen bd10_1_tmp2 = b_d5
	qui sum bd10_1_tmp2 if bd10_1_tmp2 > 1000000, de // median in the top range
	replace bd10_1_tmp2 = 62500 	if b_d6 == 4
	replace bd10_1_tmp2 = 87500 	if b_d6 == 5
	replace bd10_1_tmp2 = 150000 	if b_d6 == 8
	replace bd10_1_tmp2 = 400000 	if b_d6 == 9
	replace bd10_1_tmp2 = 750000 	if b_d6 == 10
	replace bd10_1_tmp2 = `r(p50)' 	if b_d6 == 11
	replace bd10_1_tmp2 = 0 		if b_d4 == 2

	gen bd10_1_tmp3 = b_d8 // no range update
	replace bd10_1_tmp3 = 0 		if b_d7 == 2

	gen bd10_1_tmp4 = b_d11
	replace bd10_1_tmp4 = 250000 	if b_d12 == 8
	replace bd10_1_tmp4 = 400000 	if b_d12 == 9
	replace bd10_1_tmp4 = 0 		if b_d10 == 2
	replace bd10_1_tmp4 = 0 		if b_p3 == 1

gen missing_bd10_1 = bd10_1_tmp1 ==. | bd10_1_tmp2 == . | bd10_1_tmp3 == . | bd10_1_tmp4 == .

foreach i of numlist 1 2 3 4 {
	winsor2 bd10_1_tmp`i', replace cuts(1 99)
	qui sum bd10_1_tmp`i', de
	replace bd10_1_tmp`i' = `r(mean)' 		if bd10_1_tmp`i' == .	
}

replace bd10_1 = bd10_1_tmp1 + bd10_1_tmp2 + bd10_1_tmp3 + bd10_1_tmp4

* 2. : Business survival, measured using an indicator for whether the main business is operating at the time of the survey 
// no baseline

* 3. b_d13: How much money was your household able to save in the past 30 days? 
	replace bd10_3 = b_d13
	replace bd10_3 = . 			if b_d13 == -999
	replace bd10_3 = 62500 		if b_d14 == 4
	replace bd10_3 = 250000 	if b_d14 == 8
	replace bd10_3 = 400000 	if b_d14 == 9
	replace bd10_3 = 750000 	if b_d14 == 10 
	gen missing_bd10_3 = bd10_3 == .

	winsor2 bd10_3, replace cuts(1 99)
	qui sum bd10_3, de
	replace bd10_3 = `r(mean)' 	if bd10_3 == .

* 4. b_e7: Compared to the average Ugandan in your neighborhood, how would you describe the economic situation of your household? Much better, somewhat better, about the same, somewhat worse, or much worse? 
	replace bd10_4 = b_e7
	gen missing_bd10_4 = bd10_4 == .
		qui count if bd10_4 > 3 & bd10_4 != .
		local num_high = r(N)
		qui count if bd10_4 < 3
		local num_low = r(N)
			replace bd10_4 = (bd10_4 <= 3)	if bd10_4 != . & `num_low' < `num_high'
			replace bd10_4 = (bd10_4 < 3) 	if bd10_4 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree

* 5. b_d15: Over the past 30 days, how often have you or anyone in your household gone without enough food to eat? 
	replace bd10_5 = 1 	if b_d15 == 3 | b_d15 == 4
	replace bd10_5 = 0 	if b_d15 == 1 | b_d15 == 2

* 6. b_d16: Over the past 30 days, how often have you or anyone in your household struggled to afford basic household expenses (such as medicine, rent, school fees)? 
	replace bd10_6 = 1 	if b_d16 == 3 | b_d16 == 4
	replace bd10_6 = 0 	if b_d16 == 1 | b_d16 == 2

* 7. b_d17: In the past 30 days, have you or anyone in your household had to sell assets (jewelry, furniture, clothing, tools, machines, land) in order to afford basic household expenses? 
	replace bd10_7 = 1 	if b_d17 == 2
	replace bd10_7 = 0 	if b_d17 == 1

* 8. b_d18: In the past 30 days, has your household had to stop education for a child due to lack of finances? 
	replace bd10_8 = 1 	if b_d18 == 2
	replace bd10_8 = 0 	if b_d18 == 1
	replace bd10_8 = 0 	if b_p4 == 0

foreach i of numlist 5 6 7 8 {
	gen missing_bd10_`i' = bd10_`i' == .
	qui sum bd10_`i', de
	replace bd10_`i' = `r(mean)' 		if bd10_`i' == .
}


**** Domain 11: Policy Preferences and Representation // 4 (7) outcomes
forval i = 1/4 {
	gen bd11_`i' = .
}

* 1. b_j8: Do you agree or disagree with the following statement: Uganda should accept more foreigners besides refugees. 
	replace bd11_1 = b_j8
	replace bd11_1 = 3 	if b_j8 == 6
	gen missing_bd11_1 = bd11_1 == .
		qui count if bd11_1 > 3 & bd11_1 != .
		local num_high = r(N)
		qui count if bd11_1 < 3
		local num_low = r(N)
			replace bd11_1 = (bd11_1 <= 3)	if bd11_1 != . & `num_low' < `num_high'
			replace bd11_1 = (bd11_1 < 3) 	if bd11_1 != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree

* 2. b_j9: For foreigners, besides refugees, which option do you think Uganda should follow? (analyzed as 4 binary variables) 
	replace bd11_2 = b_j9 == 1 	if b_j9 != .
	gen missing_bd11_2 = bd11_2 == .

* 3. b_c26: How satisfied are you with the LC1 for this area? 
	replace bd11_3 = b_c26

* 4. b_c27: How satisfied are you with the MP for this area? 
	replace bd11_4 = b_c27

foreach i of numlist 3 4 {
	replace bd11_`i' = . 	if bd11_`i' == 6 // no opinion / don't know / decline is coded as missing
	gen missing_bd11_`i' = bd11_`i' == .
		qui count if bd11_`i' > 3 & bd11_`i' != .
		local num_high = r(N)
		qui count if bd11_`i' < 3
		local num_low = r(N)
			replace bd11_`i' = (bd11_`i' <= 3)	if bd11_`i' != . & `num_low' < `num_high'
			replace bd11_`i' = (bd11_`i' < 3) 	if bd11_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree 
	qui sum bd11_`i', de
	replace bd11_`i' = `r(mean)' 		if bd11_`i' == .
}


**** Domain 12: Beliefs about economic effects of non-refugee immigrants // 2 outcomes
forval i = 1/2 {
	gen bd12_`i' = .
}

* 1. b_g4: Taking everything into consideration, would you say the overall economic effect of foreigners other than refugees on Uganda has been positive, negative, or neutral? 
	replace bd12_1 = b_g4

* 2. b_g5: How about the overall economic effect of foreigners other than refugees on you personally? 
	replace bd12_2 = b_g5

	
**** Domain 13: Social attitudes about other non-refugee immigrants // 1 outcome
gen bd13_1 = .

* 1. b_g6: What effect have foreigners besides refugees had on culture in Uganda? 
	replace bd13_1 = b_g6

foreach var of varlist bd12_1 bd12_2 bd13_1 {
	gen missing_`var' = `var' == .
		qui count if `var' > 3 & `var' != .
		local num_high = r(N)
		qui count if `var' < 3
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum `var', de
	replace `var' = `r(mean)' 		if `var' == .
}


**** Domain 14: Contact with Ugandans from another tribe // 8 outcomes
forval i = 1/7 {
	gen bd14_`i' = .
}

* 1. b_c2: How many of your customers are Ugandans from a different tribe? 
	replace bd14_1 = b_c2

* 2. b_c16: How many businesses in your sector in this area are managed by Ugandans from another tribe?
	replace bd14_2 = b_c16

foreach i of numlist 1 2 {
	gen missing_bd14_`i' = bd14_`i' == .
		qui count if bd14_`i' > 3 & bd14_`i' != .
		local num_high = r(N)
		qui count if bd14_`i' < 3
		local num_low = r(N)
			replace bd14_`i' = (bd14_`i' <= 3)	if bd14_`i' != . & `num_low' < `num_high'
			replace bd14_`i' = (bd14_`i' < 3) 	if bd14_`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	qui sum bd14_`i', de
	replace bd14_`i' = `r(mean)' 		if bd14_`i' == .
}

* 3. b_c10: How many of your business collaborators are Ugandans from a different tribe? 
	replace bd14_3 = b_c10
	replace bd14_3 = 0 	if b_c7 == 2
	gen missing_bd14_3 = bd14_3 == .
	qui sum bd14_3 if b_c7 == 1, de
	replace bd14_3 = `r(mean)' 	if b_c7 == 1 & bd14_3 == .

* 4. b_c11: Would you be open to collaborating with Ugandans from other tribes? 
	replace bd14_4 = 1 	if b_c11 == 1
	replace bd14_4 = 0 	if b_c11 == 2
	replace bd14_4 = 1 	if b_c10 > 0 & b_c10 != .
	gen missing_bd14_4 = bd14_4 == .
	qui sum bd14_4 if b_c7 == 1, de
	replace bd14_4 = `r(mean)' 	if bd14_4 == .

* 5. b_b15: Have you ever had an apprentice or person from outside your household at your business who was learning skills but not paid who was from another tribe? 
	replace bd14_5 = 1 	if b_b15 == 1
	replace bd14_5 = 0 	if b_b15 == 2
	replace bd14_5 = 0 	if b_b13 == 2

* 6. b_b33: Are any of your employees from a different tribe than you? 
	replace bd14_6 = 1 	if b_b33 == 1
	replace bd14_6 = 0 	if b_b33 == 2
	replace bd14_6 = 0 	if b_b31 == 0

* 7. b_e16: In the past 30 days, how many people from a different tribe have you contacted for any social reason, such as having a long conversation? 
	replace bd14_7 = b_e16

foreach i of numlist 5 6 7 {
	gen missing_bd14_`i' = bd14_`i' == .
	qui sum bd14_`i', de
	replace bd14_`i' = `r(mean)' 		if bd14_`i' == .
}

* 8. BN module: Number of people from another tribe listed in the networks module.
	gen bd14_8_tmp1 = 1 	if (((b_bn5___1 != b_p9) & (b_bn5___1 != -44 & b_bn5___1 != -45)) | (b_bn5___1 == -44 | b_bn5___1 == -45)) & b_bn5___1 != .
	gen bd14_8_tmp2 = 1 	if (((b_bn5___2 != b_p9) & (b_bn5___2 != -44 & b_bn5___2 != -45)) | (b_bn5___2 == -44 | b_bn5___2 == -45)) & b_bn5___2 != .
	gen bd14_8_tmp3 = 1 	if (((b_bn5___3 != b_p9) & (b_bn5___3 != -44 & b_bn5___3 != -45)) | (b_bn5___3 == -44 | b_bn5___3 == -45)) & b_bn5___3 != .
	gen bd14_8_tmp4 = 1 	if (((b_bn5___4 != b_p9) & (b_bn5___4 != -44 & b_bn5___4 != -45)) | (b_bn5___4 == -44 | b_bn5___4 == -45)) & b_bn5___4 != .
	egen bd14_8 = rowtotal(bd14_8_tmp1 bd14_8_tmp2 bd14_8_tmp3 bd14_8_tmp4)
	drop bd14_8_tmp*
	gen missing_bd14_8 = bd14_8 == .

	
**** Domain 15: Beliefs about economic effects of Ugandans from another tribe // 1 outcome
gen bd15_1 = .

* 1. b_c22: How do the businesses managed by Ugandans from a different tribe affect your business overall? Do they help you a lot, help you a little, hurt you a little, hurt you a lot, or have no effect on you?
	gen b_c21_tmp = b_c21
	replace b_c21_tmp = 3 	if b_c15 == 5
	replace bd15_1 = 1		if b_c22 <= b_c21_tmp & b_c22 != . & b_c21_tmp !=. // compare ugandan business influence w/ other tribe business influence
	replace bd15_1 = 0 		if b_c22 > b_c21_tmp & b_c22 != . & b_c21_tmp !=.
	drop b_c21_tmp
	gen missing_bd15_1 = bd15_1 == .
	qui sum bd15_1, de
	replace bd15_1 = `r(mean)' 	if bd15_1 == .

	
**** Domain 16: Social attitudes about Ugandans from another tribe // 1 outcome
gen bd16_1 = .

* 1. b_k1: I would be comfortable marrying a Ugandan from another tribe.
* 2. b_k2: I would be comfortable having a Ugandan from another tribe marry a member of my family.
* 3. b_k3: I would be comfortable having a Ugandan from another tribe as a close, personal friend.
* 4. b_k4: I would be comfortable having a Ugandan from another tribe as a neighbor.

	gen bd16_1_tmp1 = b_k1
	gen bd16_1_tmp2 = b_k2
	gen bd16_1_tmp3 = b_k3
		replace bd16_1_tmp3 = 1 	if b_k1 == 1 & b_k2 == 1
	gen bd16_1_tmp4 = b_k4
		replace bd16_1_tmp4 = 1 	if b_k1 == 1 & b_k2 == 1

foreach i of numlist 1 2 3 4 {
	replace bd16_1_tmp`i' = 3 	if bd16_1_tmp`i' == 6	
		qui count if bd16_1_tmp`i' > 3 & bd16_1_tmp`i' != .
		local num_high = r(N)
		qui count if bd16_1_tmp`i' < 3
		local num_low = r(N)
			replace bd16_1_tmp`i' = (bd16_1_tmp`i' <= 3)	if bd16_1_tmp`i' != . & `num_low' < `num_high'
			replace bd16_1_tmp`i' = (bd16_1_tmp`i' < 3) 	if bd16_1_tmp`i' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
} // = 1 if positive; = 0 if negative

swindex bd16_1_tmp1 bd16_1_tmp2 bd16_1_tmp3 bd16_1_tmp4, generate(bd16_1) normby(control) replace

gen missing_bd16_1 = bd16_1 == .

foreach var of varlist bd16_1_tmp* {
	gen missing_`var' = `var' == .
}

**** Domain 17: Gender roles // 3 outcomes
forval i = 1/3 {
	gen bd17_`i' = .
}

* 1. b_d2: Do you share all of your profits from this business with your spouse? 
	replace bd17_1 = 1 	if b_d2 == 1
	replace bd17_1 = 0 	if b_d2 == 2
	replace bd17_1 = 0 	if b_p2 != 1
	gen missing_bd17_1 = bd17_1 == .
	
	gen bd17_1_female = bd17_1 	if ls_b4 == 2
	gen bd17_1_male = bd17_1 	if ls_b4 == 1	
	gen missing_bd17_1_female = bd17_1_female == .
	gen missing_bd17_1_male = bd17_1_male == .
	

* 2. b_d3: Who decides how the household’s money is spent?
	replace bd17_2 = 1 	if (b_d3 == 1 | b_d3 == 2 | b_d3 == 5) & ls_b4 == 2 // women
	replace bd17_2 = 0 	if (b_d3 == 3 | b_d3 == 4) & ls_b4 == 2
	gen missing_bd17_2 = bd17_2 == .
	qui sum bd17_2, de
	replace bd17_2 = `r(mean)' 	if bd17_2 == . & ls_b4 == 2

	replace bd17_3 = 1 	if (b_d3 == 1 | b_d3 == 2 | b_d3 == 5) & ls_b4 == 1 // men
	replace bd17_3 = 0 	if (b_d3 == 3 | b_d3 == 4) & ls_b4 == 1
	gen missing_bd17_3 = bd17_3 == .
	qui sum bd17_3, de
	replace bd17_3 = `r(mean)' 	if bd17_3 == . & ls_b4 == 1

* 1. : In total, about how much income did your family earn during the 4 months of the lockdown (April - July)? Do not count money that you borrowed. 
* 2. : During the lockdown, how often did you or anyone in your household go without enough food to eat? Always, often, sometimes, or never? 
* 3. : During the lockdown, how often did you or anyone in your household struggle to afford basic household expenses other than food (such as medicine, rent, school fees)? 
* 4. : During the lockdown, did you or anyone in your household have to sell assets (jewelry, furniture, clothing, tools, machines, land) in order to afford basic household expenses? 
* 5. : How much did you borrow during the lockdown to pay for basic necessities like food, housing, and medicine? 

***** Domain 20: Importance of Tribe // 1 outcome
	gen bd20_1 = b_p11
	replace bd20_1 = (bd20_1 < 3) if bd20_1 != .
	gen missing_bd20_1 = (bd20_1 != .)

*==== Monetary measures ====*
foreach var of varlist bd2_1 bd9_2 bd9_5 bd10_1 bd10_3 {
	gen `var'_stat = `var' / 3695 // for statistics
	replace `var' = asinh(`var'_stat)
}

end
Baseline


*++++ Gen ToT variables ++++*
capture program drop ToTvar
program ToTvar

***** Generate actual treatment status variable
destring any_mentorship_meeting yg_meeting_num_pre yg_meeting_num_post, replace ignore("NA")

egen yg_meeting_num = rowtotal(yg_meeting_num_pre yg_meeting_num_post)

gen treated = .
	replace treated = 1 	if treatment == 1 & any_mentorship_meeting == 1
	replace treated = 6 	if treatment == 1 & any_mentorship_meeting == 0
	replace treated = 2 	if treatment == 2 & any_mentorship_meeting == 1
	replace treated = 6 	if treatment == 2 & any_mentorship_meeting == 0
	replace treated = 3 	if treatment == 3 & (date_cash_received_2021 != "NA" | date_cash_received_2020 != "NA")
	replace treated = 6 	if treatment == 3 & (date_cash_received_2021 == "NA" & date_cash_received_2020 == "NA")
	replace treated = 4 	if treatment == 4 & yg_meeting_num > 0
	replace treated = 6 	if treatment == 4 & yg_meeting_num == 0
	replace treated = 5 	if treatment == 5 & (date_cash_received_2021 != "NA" | date_cash_received_2020 != "NA")
	replace treated = 6 	if treatment == 5 & (date_cash_received_2021 == "NA" & date_cash_received_2020 == "NA")
	replace treated = 6 	if treatment == 6

	label define treated1 1 "U-Mentee" 2 "R-Mentee" 3 "Cash" 4 "Info" 5 "Cash+Info" 6 "Control/Not Treated" 
	label values treated treated1
end
ToTvar


*++++ Gen treatment month variables ++++*
capture program drop TreatmentMonth
program TreatmentMonth

***** Treated month

** Cash received month (relevant for Cash and Cash+Info treatment arms) // date format: DD-MMM-YY
gen date_cash_received = ""
	replace date_cash_received = date_cash_received_2020 	if date_cash_received_2020 != "NA"
	replace date_cash_received = date_cash_received_2021 	if date_cash_received_2021 != "NA" & date_cash_received_2020 == "NA"

split date_cash_received, p(-) g(date_cash_received__)

gen cash_received_month = date_cash_received__2
gen cash_received_year = date_cash_received__3
	drop date_cash_received__*
	replace cash_received_month = "2" 	if cash_received_month == "Feb"
	replace cash_received_month = "3" 	if cash_received_month == "Mar"
	replace cash_received_month = "4" 	if cash_received_month == "Apr"
	replace cash_received_month = "5" 	if cash_received_month == "May"
	destring cash_received_month cash_received_year, replace
	gen date_cash_received_num = date(date_cash_received, "DMY", 2050)

** First meet month (relevant for Cash, Cash+Info, and Info treatment arms) 
// date format: first_meet_*: irregular (needs to be formatted first to DD-MMM-YY), second_meet_*: DD-MMM-YY
// generate earliest meet data among first_meet_2020, second_meet_2020, first_meet_2021, second_meet_2021

* Format date variables to DD-MMM-YY
forval yr = 2020/2021 {
	gen first_meet_`yr'_tmp = first_meet_`yr'
	gen first_meet_`yr'_tmp2 = substr(first_meet_`yr'_tmp, 1, strpos(first_meet_`yr', " ") - 1)
		replace first_meet_`yr'_tmp = first_meet_`yr'_tmp2 	if first_meet_`yr'_tmp2 != ""
	gen first_meet_`yr'_flag = strpos(first_meet_`yr'_tmp, "`yr'") > 0
		split first_meet_`yr'_tmp, p(-) g(first_meet_`yr'_)
}

gen first_meet_2020_tmp3 = first_meet_2020_3 + "-" + first_meet_2020_2 + "-20" 	if first_meet_2020_flag == 1
gen first_meet_2021_tmp3 = first_meet_2021_3 + "-" + first_meet_2021_2 + "-21" 	if first_meet_2021_flag == 1
	replace first_meet_2020_tmp3 = first_meet_2020_tmp 	if first_meet_2020_flag == 0
	replace first_meet_2021_tmp3 = first_meet_2021_tmp 	if first_meet_2021_flag == 0
	drop first_meet_*_tmp first_meet_*_tmp2 first_meet_*_flag first_meet_*_1 first_meet_*_2 first_meet_*_3
	ren first_meet_*_tmp3 first_meet_*_format

* Generate first meet month and year variables
gen first_meet = ""
	replace first_meet = first_meet_2020_format 	if first_meet_2020_format != "NA"
	replace first_meet = second_meet_2020 			if first_meet == "" & second_meet_2020 != "NA"
	replace first_meet = first_meet_2021_format 	if first_meet == "" & first_meet_2021_format != "NA"
	replace first_meet = second_meet_2021 			if first_meet == "" & second_meet_2021 != "NA"

split first_meet, p(-) g(first_meet__)

gen first_meet_month = first_meet__2
gen first_meet_year = first_meet__3
	drop first_meet__*
	replace first_meet_month = "1" 	if first_meet_month == "Jan"	
	replace first_meet_month = "2" 	if first_meet_month == "Feb"
	replace first_meet_month = "3" 	if first_meet_month == "Mar"
	replace first_meet_month = "4" 	if first_meet_month == "Apr"	
	replace first_meet_month = "5" 	if first_meet_month == "May"
	destring first_meet_month first_meet_year, replace
	gen first_meet_num = date(first_meet, "DMY", 2050)
	
** Mentorship meeting variables
* Total number of mentorship meetings
replace num_mentorship_meeting = "" 	if num_mentorship_meeting == "NA"
	destring num_mentorship_meeting, gen(num_mentorship_meeting_destring)
	la var num_mentorship_meeting "Total num of mentorship meetings"
	
* Total number of in-person phone mentorship meetings
foreach var of varlist men_* {
	gen `var'_flag = `var' != "NA"
	split `var', p(" ") g(`var'__)
		gen `var'_date = date(`var'__1, "MDY") 	if `var' != "NA"
		drop `var'__1 `var'__2
}

// meetings in 2020 were in person, meetings in 2021 were over the phone
egen num_mentorship_inperson = rowtotal(men_*_2020_flag)
	la var num_mentorship_inperson "Total num of in-person mentorship meetings"
egen num_mentorship_phone = rowtotal(men_*_2021_flag)
	la var num_mentorship_phone "Total num of phone mentorship meetings"

egen first_mentorship_meeting_date = rowmin(men_*_2020_date men_*_2021_date)
	la var first_mentorship_meeting_date "Date of first mentorship meeting"
egen first_mentorship_inperson_date = rowmin(men_*_2020_date)
	la var first_mentorship_inperson_date "Date of first mentorship meeting"
egen first_mentorship_phone_date = rowmin(men_*_2021_date)
	la var first_mentorship_phone_date "Date of first mentorship meeting"	

* Number of mentorship meetings through the time of the survey
foreach var of varlist men_*_date {
	gen `var'_b = (`var' <= survey_date) 	if `var' != . & survey_date != .
}

egen num_meeting_through_survey = rowtotal(men_*_date_b)
	
	
** Generate treated period
// survey month: midline (oct 2020), endline (may 2021), phone2 (aug 2021), endline2 (mar 2022)

* Version 1 months between survey and treatment: cash received date for Cash and Cash+Info arms, first YARID meet date for Info arm, total num of mentorship meeting for U-mentor and R-mentor arms
gen treated_month_v1 = .
	replace treated_month_v1 = cash_received_month 	if treated == 3 | treated == 5
	replace treated_month_v1 = first_meet_month 	if treated == 4
gen treated_year_v1 = .
	replace treated_year_v1 = cash_received_year 	if treated == 3 | treated == 5
	replace treated_year_v1 = first_meet_year 		if treated == 4

gen treated_period_v1 = .
	replace treated_period_v1 = (20 - treated_year_v1) * 12 + (10 - treated_month_v1) 	if wave == 1 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v1 = (21 - treated_year_v1) * 12 + (5 - treated_month_v1) 	if wave == 2 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v1 = (21 - treated_year_v1) * 12 + (8 - treated_month_v1) 	if wave == 3 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v1 = (22 - treated_year_v1) * 12 + (3 - treated_month_v1) 	if wave == 4 & (treated == 3 | treated == 4 | treated == 5)	
	
	replace treated_period_v1 = num_meeting_through_survey 	if (treated == 1 | treated == 2) & wave != .
	replace treated_period_v1 = 0 	if treated == 6 & wave != .
	replace treated_period_v1 = 0 	if treated_period_v1 < 0

gen treated_v1 = .
	replace treated_v1 = 1 	if survey_date >= date_cash_received_num & survey_date != . & date_cash_received_num != . & (treated == 3 | treated == 5)
	replace treated_v1 = 0 	if survey_date < date_cash_received_num & survey_date != . & date_cash_received_num != . & (treated == 3 | treated == 5)
	replace treated_v1 = 1 	if survey_date >= first_meet_num & survey_date != . & first_meet_num != . & treated == 4
	replace treated_v1 = 0 	if survey_date < first_meet_num & survey_date != . & first_meet_num != . & treated == 4		
	replace treated_v1 = 1 	if survey_date >= first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v1 = 0 	if survey_date < first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v1 = 0 	if treated == 6 & wave != .

* Version 2 months between survey and treatment: first YARID meet date for Cash, Cash+Info, and Info arms, total num of mentorship meeting for U-mentor and R-mentor arms
gen treated_month_v2 = .
	replace treated_month_v2 = first_meet_month 	if treated == 3 | treated == 4 | treated == 5
gen treated_year_v2 = .
	replace treated_year_v2 = first_meet_year 		if treated == 3 | treated == 4 | treated == 5

gen treated_period_v2 = .
	replace treated_period_v2 = (20 - treated_year_v2) * 12 + (10 - treated_month_v2) 	if wave == 1 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v2 = (21 - treated_year_v2) * 12 + (5 - treated_month_v2) 	if wave == 2 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v2 = (21 - treated_year_v2) * 12 + (8 - treated_month_v2) 	if wave == 3 & (treated == 3 | treated == 4 | treated == 5)
	replace treated_period_v2 = (22 - treated_year_v2) * 12 + (3 - treated_month_v2) 	if wave == 4 & (treated == 3 | treated == 4 | treated == 5)	
	
	replace treated_period_v2 = num_meeting_through_survey 	if (treated == 1 | treated == 2) & wave != .
	replace treated_period_v2 = 0 	if treated == 6 & wave != .
	replace treated_period_v2 = 0 	if treated_period_v2 < 0

gen treated_v2 = .
	replace treated_v2 = 1 	if survey_date >= first_meet_num & survey_date != . & first_meet_num != . & (treated == 3 | treated == 4 | treated == 5)
	replace treated_v2 = 0 	if survey_date < first_meet_num & survey_date != . & first_meet_num != . & (treated == 3 | treated == 4 | treated == 5)	
	replace treated_v2 = 1 	if survey_date >= first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v2 = 0 	if survey_date < first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v2 = 0 	if treated == 6 & wave != .

* Version 3 months between survey and treatment: cash received date for Cash and Cash+Info arms, first YARID meet date for Info arm, total num of in-person mentorship meeting for U-mentor and R-mentor arms
gen treated_period_v3 = .
	replace treated_period_v3 = num_mentorship_inperson if (treated == 1 | treated == 2) & wave != .
	replace treated_period_v3 = 1 						if (treated == 3 | treated == 5) & cash_received_year == 21 & wave != .
	replace treated_period_v3 = 0 						if (treated == 3 | treated == 5) & cash_received_year == 20 & wave != .
	replace treated_period_v3 = 1 						if treated == 4 & first_meet_year == 21 & wave != .
	replace treated_period_v3 = 0 						if treated == 4 & first_meet_year == 20 & wave != .
	replace treated_period_v3 = 0 						if treated == 6 & wave != .

gen treated_v3 = .
	replace treated_v3 = treated_v1 	if treated == 3 | treated == 4 | treated == 5 | treated == 6
	replace treated_v3 = 1 	if survey_date >= first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v3 = 0 	if survey_date < first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	
* Version 4 months between survey and treatment: first YARID meet date for Cash, Cash+Info, and Info arms, total num of in-person mentorship meeting for U-mentor and R-mentor arms
gen treated_period_v4 = .
	replace treated_period_v4 = num_mentorship_inperson if (treated == 1 | treated == 2) & wave != .	
	replace treated_period_v4 = 1 						if (treated == 3 | treated == 4 | treated == 5) & first_meet_year == 21 & wave != .
	replace treated_period_v4 = 0 						if (treated == 3 | treated == 4 | treated == 5) & first_meet_year == 20 & wave != .
	replace treated_period_v4 = 0 						if treated == 6 & wave != .
	
gen treated_v4 = .
	replace treated_v4 = treated_v2 	if treated == 3 | treated == 4 | treated == 5 | treated == 6
	replace treated_v4 = 1 	if survey_date >= first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)
	replace treated_v4 = 0 	if survey_date < first_mentorship_meeting_date & survey_date != . & first_mentorship_meeting_date != . & (treated == 1 | treated == 2)

gen treated_refugee = treated_v2
	
end
TreatmentMonth


*++++ Gen Anderson summary index ++++*
capture program drop Anderson
program Anderson

cap gen control = treatment == 6 // = 1 for untreated group, used in swindex construction

***** Domain 1: Support for inclusive refugee hosting
	swindex bd1_1 bd1_3 bd1_5 bd1_7, gen(b_domain1) replace normby(control) // normalizes by full control group, not those we found
	swindex d1_1 d1_2 d1_3 d1_4 d1_5 d1_6 d1_7 d1_8 	if wave == 1, gen(e_domain1) replace normby(control)
	swindex d1_1 d1_2 d1_3 d1_4 d1_5 d1_7 d1_8 			if wave == 2, gen(e_domain1b) replace normby(control)
	swindex d1_1 d1_3 d1_5 d1_7 						if wave == 4, gen(e_domain1e2) replace normby(control)
	replace e_domain1 = e_domain1b 	if wave == 2
	replace e_domain1 = e_domain1e2 if wave == 4
	la var e_domain1 "\shortstack{Integration\\Policies\\Index}"
	
	swindex d1_1 d1_3 d1_5 d1_7 	if wave == 1, gen(e_domain1_fixed) replace normby(control) //version that fixes components across surveys to check for timing differences in domains 1, 4, 6
	swindex d1_1 d1_3 d1_5 d1_7 	if wave == 2, gen(e_domain1b_fixed) replace normby(control)
	swindex d1_1 d1_3 d1_5 d1_7 	if wave == 4, gen(e_domain1e2_fixed) replace normby(control)
	replace e_domain1_fixed = e_domain1b_fixed 	if wave == 2
	replace e_domain1_fixed = e_domain1e2_fixed if wave == 4
	la var e_domain1_fixed "\shortstack{Integration\\Policies\\Index}"

	swindex bd1_1 bd1_3 bd1_5 bd1_7 bd1a_9 bd1a_10 bd1a_11, gen(b_domain111) replace normby(control) 
	swindex d1_1 d1_2 d1_3 d1_4 d1_5 d1_7 d1_8 d1a_9 d1a_10 d1a_11 	if wave == 2, gen(e_domain111) replace normby(control)
	swindex d1_1 d1_3 d1_5 d1_7 d1a_9 d1a_10 d1a_11 				if wave == 4, gen(e_domain112) replace normby(control) 
	replace e_domain111 = e_domain1 	if wave == 1
	replace e_domain111 = e_domain112 	if wave == 4
	la var e_domain111 "\shortstack{Complete\\Index}"
	
	swindex bd1_1 bd1_3 bd1_5 bd1_7 bd1a_9 bd1a_10 bd1a_11, gen(b_domain1111) replace normby(control) 
	swindex d1_1 d1_2 d1_3 d1_4 d1_5 d1_7 d1_8 d1a_9 d1a_10 d1a_11 			if wave == 2, gen(e_domain1111) replace normby(control)
	swindex d1_1 d1_3 d1_5 d1_7 d1a_9 d1a_10 d1a_11 d1a_12 d1a_13 d1a_14	if wave == 4, gen(e_domain1112) replace normby(control) 
	replace e_domain1111 = e_domain1 		if wave == 1
	replace e_domain1111 = e_domain1112 	if wave == 4
	la var e_domain1111 "\shortstack{Complete\\Index 2}"	

***** Domain 2: Business profits
	swindex bd2_1, gen(b_domain2) replace normby(control)
	swindex d2_1 	if wave == 1, gen(e_domain2) replace normby(control)
	swindex d2_1 	if wave == 2, gen(e_domain2b) replace normby(control)
	swindex d2_1 	if wave == 3, gen(e_domain2p) replace normby(control)
	swindex d2_1 	if wave == 4, gen(e_domain2e2) replace normby(control)
	replace e_domain2 = e_domain2b	 if wave == 2
	replace e_domain2 = e_domain2p	 if wave == 3
	replace e_domain2 = e_domain2e2	 if wave == 4
	la var e_domain2 "\shortstack{Profit\\(Standardized)}"

***** Domain 3: Knowledge of refugees and hosting policy
	swindex bd3_1 bd3_2 bd3_3, gen(b_domain3) replace normby(control)
	swindex d3_3 					if wave == 1, gen(e_domain3) replace normby(control)
	swindex d3_1 d3_2 d3_3 			if wave == 2, gen(e_domain3b) replace normby(control)
	swindex d3_1 d3_2 d3_3 			if wave == 4, gen(e_domain3e) replace normby(control)
	replace e_domain3 = e_domain3b 	if wave == 2
	replace e_domain3 = e_domain3e 	if wave == 4
	la var e_domain3 "\shortstack{Knowledge\\Index}"

***** Domain 4: Beliefs about economic effects of refugees
	swindex bd4_1 bd4_2 bd4_3 bd4_4, gen(b_domain4) replace normby(control)
	swindex d4_2 d4_3 				if wave == 1, gen(e_domain4) replace normby(control)
	swindex d4_1 d4_2 d4_3 d4_4 	if wave == 2, gen(e_domain4b) replace normby(control)
	swindex d4_1 d4_2 d4_3 d4_4 	if wave == 4, gen(e_domain4e) replace normby(control)
	replace e_domain4 = e_domain4b 	if wave == 2
	replace e_domain4 = e_domain4e 	if wave == 4
	la var e_domain4 "\shortstack{Economic\\Beliefs\\Index}"
	
	swindex d4_2 d4_3 				if wave == 1, gen(e_domain4_fixed) replace normby(control) //version that fixes components across surveys to check for timing differences in domains 1, 4, 6
	swindex d4_2 d4_3 				if wave == 2, gen(e_domain4b_fixed) replace normby(control)
	swindex d4_2 d4_3 				if wave == 4, gen(e_domain4e_fixed) replace normby(control)
	replace e_domain4_fixed  = e_domain4b_fixed 	if wave == 2
	replace e_domain4_fixed  = e_domain4e_fixed 	if wave == 4
	la var e_domain4_fixed "\shortstack{Economic\\Beliefs\\Index}"

***** Domain 4a: Beliefs about economic effects of Congolese refugees
	swindex bd4a_1 bd4a_2 bd4a_3 bd4a_4, gen(b_domain41) replace normby(control)
	swindex d4a_1 d4a_2 d4a_3 d4a_4 	if wave == 2, gen(e_domain41) replace normby(control) 
	swindex d4a_1 d4a_2 d4a_3 d4a_4 	if wave == 4, gen(e_domain41e) replace normby(control)
	replace e_domain41 = e_domain41e 	if wave == 4
	la var e_domain41 "\shortstack{Economic\\Beliefs\\Index}"

***** Domain 4b: Beliefs about economic effects of Somali refugees
	swindex bd4b_1 bd4b_2 bd4b_3 bd4b_4, gen(b_domain42) replace normby(control)
	swindex d4b_1 d4b_2 d4b_3 d4b_4 	if wave == 2, gen(e_domain42) replace normby(control)
	swindex d4b_1 d4b_2 d4b_3 d4b_4 	if wave == 4, gen(e_domain42e) replace normby(control)
	replace e_domain42 = e_domain42e 	if wave == 4
	la var e_domain42 "\shortstack{Economic\\Beliefs\\Index}"

***** Domain 5a: Beliefs that refugees receive too much aid
	swindex bd5a_1 bd5a_2, gen(b_domain51) replace normby(control)
	swindex d5a_1 d5a_2 				if wave == 2, gen(e_domain51) replace normby(control)
	swindex d5a_1 d5a_2					if wave == 4, gen(e_domain51e) replace normby(control)
	replace e_domain51 = e_domain51e 	if wave == 4
	la var e_domain51 "\shortstack{Economic\\Perceptions\\Index}"

***** Domain 5b: Beliefs that refugees can support themselves
	swindex bd5b_1, gen(b_domain52) replace normby(control)
	swindex d5b_1 						if wave == 2, gen(e_domain52) replace normby(control)
	swindex d5b_1 						if wave == 4, gen(e_domain52e) replace normby(control)
	replace e_domain52 = e_domain52e 	if wave == 4
	la var e_domain52 "\shortstack{Economic\\Perceptions\\Index}"

***** Domain 6: Social attitudes about refugees
	swindex bd6_1 bd6_2 bd6_3 bd6_4 bd6_6 bd6_7, gen(b_domain6) replace normby(control)
	swindex d6_1 d6_4 						if wave == 1, gen(e_domain6) replace normby(control)
	swindex d6_1 d6_2 d6_3 d6_4 d6_6 d6_7 	if wave == 2, gen(e_domain6b) replace normby(control)
	swindex d6_1 d6_2 d6_3 d6_4 d6_6 d6_7 	if wave == 4, gen(e_domain6e) replace normby(control) 
	replace e_domain6 = e_domain6b  	if wave == 2
	replace e_domain6 = e_domain6e 		if wave == 4
	la var e_domain6 "\shortstack{Cultural\\Attitudes\\Index}"
	
	swindex d6_1 d6_4 				if wave == 1, gen(e_domain6_fixed) replace normby(control)
	swindex d6_1 d6_4  				if wave == 2, gen(e_domain6b_fixed) replace normby(control)
	swindex d6_1 d6_4  				if wave == 4, gen(e_domain6e_fixed) replace normby(control) 
	replace e_domain6_fixed = e_domain6b_fixed  	if wave == 2
	replace e_domain6_fixed = e_domain6e_fixed 		if wave == 4
	la var e_domain6_fixed "\shortstack{Cultural\\Attitudes\\Index}"
	
	
***** Domain 6a: Social attitudes about Congolese refugees
	swindex bd6a_1 bd6a_2 bd6a_3 bd6a_4, gen(b_domain61) replace normby(control)
	swindex d6a_1 d6a_2 d6a_3 d6a_4 	if wave == 2, gen(e_domain61) replace normby(control)
	swindex d6a_1 d6a_2 d6a_3 d6a_4 	if wave == 4, gen(e_domain61e) replace normby(control)
	replace e_domain61 = e_domain61e 	if wave == 4
	la var e_domain61 "\shortstack{Cultural\\Attitudes\\Index}"

***** Domain 6b: Social attitudes about Somali refugees
	swindex bd6b_1 bd6b_2 bd6b_3 bd6b_4, gen(b_domain62) replace normby(control)
	swindex d6b_1 d6b_2 d6b_3 d6b_4 	if wave == 2, gen(e_domain62) replace normby(control)
	swindex d6b_1 d6b_2 d6b_3 d6b_4 	if wave == 4, gen(e_domain62e) replace normby(control)
	replace e_domain62 = e_domain62e 	if wave == 4
	la var e_domain62 "\shortstack{Cultural\\Attitudes\\Index}"

***** Domain 6c: Alternative Donation Measures
	swindex bd6c_4 bd6c_5, gen(b_domain63) replace normby(control)
	swindex d6c_4 d6c_5 				if wave == 1, gen(e_domain63) replace normby(control)
	swindex d6c_4 d6c_5 				if wave == 2, gen(e_domain63b) replace normby(control)	
	swindex d6c_4 d6c_5 				if wave == 4, gen(e_domain63e) replace normby(control)
	replace e_domain63 = e_domain63b 	if wave == 2
	replace e_domain63 = e_domain63e 	if wave == 4
	la var e_domain63 "\shortstack{Donation\\Index}"

***** Domain 7: Contact with refugees by choice
	swindex bd7_1 bd7_2 bd7_3 bd7_4 bd7_5 bd7_6 bd7_7, gen(b_domain7) replace normby(control)
	swindex d7_1 d7_2 d7_3 d7_4 d7_5 d7_6 d7_7 	if wave == 2, gen(e_domain7) replace normby(control)
	swindex d7_1 d7_2 d7_3 d7_4 d7_5 d7_6 d7_7 	if wave == 4, gen(e_domain7e) replace normby(control)
	replace e_domain7 = e_domain7e 				if wave == 4
	la var e_domain7 "\shortstack{Contact\\Refugees\\by Choice\\Index}"

***** Domain 8: Contact with refugees by circumstance
	swindex bd8_1 bd8_2 bd8_3, gen(b_domain8) replace normby(control)
	swindex d8_1 d8_2 d8_3 			if wave == 2, gen(e_domain8) replace normby(control)
	swindex d8_1 d8_2 d8_3 			if wave == 4, gen(e_domain8e) replace normby(control)
	replace e_domain8 = e_domain8e 	if wave == 4
	la var e_domain8 "\shortstack{Contact\\Refugees\\by Circumst.\\Index}"

***** Domain 9: Business practices 
	swindex bd9_2 bd9_3 bd9_5 bd9_6 bd9_7 bd9_8 bd9_9 bd9_10, gen(b_domain9) replace normby(control)
	swindex d9_2 d9_3 d9_5 d9_6 d9_7 d9_8 d9_9 d9_10 	if wave == 2, gen(e_domain9) replace normby(control) 
	swindex d9_2 d9_3 d9_5 d9_6 d9_7 d9_8 d9_9 d9_10 	if wave == 4, gen(e_domain9e) replace normby(control) 
	replace e_domain9 = e_domain9e 	if wave == 4
	la var e_domain9 "\shortstack{Business\\Practices\\Index}"

	// TG's comment: took out debt, ambiguous (generated per Travis' email on July 27, 2022)
	swindex bd9_2 bd9_3 bd9_6 bd9_7 bd9_8 bd9_9 bd9_10, gen(b_domain9_alt) replace normby(control)
	swindex d9_2 d9_3 d9_6 d9_7 d9_8 d9_9 d9_10 	if wave == 2, gen(e_domain9_alt) replace normby(control) 
	swindex d9_2 d9_3 d9_6 d9_7 d9_8 d9_9 d9_10 	if wave == 4, gen(e_domain9_alte) replace normby(control) 
	replace e_domain9_alt = e_domain9_alte 	if wave == 4
	la var e_domain9 "\shortstack{Business\\Practices\\Index}"	
	
***** Domain 9a: Business practices (marketing)
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 d9a_7 	if wave == 2, gen(e_domain91) replace normby(control)
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 d9a_7 	if wave == 4, gen(e_domain91e) replace normby(control)
	replace e_domain91 = e_domain91e 	if wave == 4
	la var e_domain91 "\shortstack{Marketing\\Index$^+$}"

***** Domain 9b: Business practices (stock)
	swindex d9b_1 d9b_2 d9b_3 			if wave == 2, gen(e_domain92) replace normby(control)
	swindex d9b_1 d9b_2 d9b_3 			if wave == 4, gen(e_domain92e) replace normby(control)
	replace e_domain92 = e_domain92e 	if wave == 4
	la var e_domain92 "\shortstack{Stock\\Index$^+$}"

***** Domain 9c: Business practices (record-keeping)
	swindex d9c_1 d9c_2 d9c_3 			if wave == 2, gen(e_domain93) replace normby(control)
	swindex d9c_1 d9c_2 d9c_3 			if wave == 4, gen(e_domain93e) replace normby(control)
	replace e_domain93 = e_domain93e 	if wave == 4
	la var e_domain93 "\shortstack{Record-Keeping\\Index$^+$}"

***** Domain 9d: Business practices // 13 outcomes
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 d9a_7 d9b_1 d9b_2 d9b_3 d9c_1 d9c_2 d9c_3 	if wave == 2, gen(e_domain94) replace normby(control)
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 d9a_7 d9b_1 d9b_2 d9b_3 d9c_1 d9c_2 d9c_3 	if wave == 4, gen(e_domain94e) replace normby(control) //
	replace e_domain94 = e_domain94e 	if wave == 4
	la var e_domain94 "\shortstack{Business\\Practices\\Index$^+$}"

***** Domain 9e: Business practices (report changing practices) // 5 outcomes
	swindex d9e_1 d9e_2 d9e_3 d9e_4 d9e_5 	if wave == 2, gen(e_domain95) replace normby(control)
	la var e_domain95 "\shortstack{Change\\Index$^+$}"

***** Domain 10: Household Well-Being
	swindex bd10_1 bd10_3 bd10_4 bd10_5 bd10_6 bd10_7 bd10_8, gen(b_domain10) replace normby(control)
	swindex d10_2 d10_5 d10_6 									if wave == 1, gen(e_domain10) replace normby(control)
	swindex d10_1 d10_2 d10_3 d10_4 d10_5 d10_6 d10_7 d10_8 	if wave == 2, gen(e_domain10b) replace normby(control)
	swindex d10_1 d10_2 d10_3 d10_5 d10_6 d10_7 d10_8			if wave == 3, gen(e_domain10p) replace normby(control)
	swindex d10_1 d10_2 d10_3 d10_4 d10_5 d10_6 d10_7 d10_8 	if wave == 4, gen(e_domain10e) replace normby(control)
	replace e_domain10 = e_domain10b 	if wave == 2
	replace e_domain10 = e_domain10p 	if wave == 3
	replace e_domain10 = e_domain10e 	if wave == 4
	la var e_domain10 "\shortstack{Household\\Well-Being\\Index}"

***** Domain 11: Policy Preferences and Representation
	swindex bd11_1 bd11_2 bd11_3 bd11_4, gen(b_domain11) replace normby(control)
	swindex d11_4 							if wave == 1, gen(e_domain11) replace normby(control)
	swindex d11_1 d11_2 d11_3 d11_4 		if wave == 2, gen(e_domain11b) replace normby(control)
	swindex d11_3 d11_4						if wave == 3, gen(e_domain11p) replace normby(control)
	swindex d11_1 d11_2 d11_3 d11_4			if wave == 4, gen(e_domain11e) replace normby(control)
	replace e_domain11 = e_domain11b 	if wave == 2
	replace e_domain11 = e_domain11p 	if wave == 3
	replace e_domain11 = e_domain11e	if wave == 4
	la var e_domain11 "\shortstack{General\\Policy\\Index}"

***** Domain 12: Beliefs about economic effects of non-refugee immigrants
	swindex bd12_1 bd12_2, gen(b_domain12) replace normby(control)
	swindex d12_1 d12_2 				if wave == 2, gen(e_domain12) replace normby(control)
	swindex d12_1 d12_2 				if wave == 4, gen(e_domain12e) replace normby(control)
	replace e_domain12 = e_domain12e 	if wave == 4
	la var e_domain12 "\shortstack{Foreigners:\\Economic\\Beliefs\\Index}"

***** Domain 13: Social attitudes about other non-refugee immigrants
	swindex bd13_1, gen(b_domain13) replace normby(control)
	swindex d13_1 						if wave == 2, gen(e_domain13) replace normby(control)
	swindex d13_1 						if wave == 4, gen(e_domain13e) replace normby(control)	
	replace e_domain13 = e_domain13e 	if wave == 4
	la var e_domain13 "\shortstack{Foreigners:\\Cultural\\Attitudes\\Index}"

***** Domain 14: Contact with Ugandans from another tribe
	swindex bd14_1 bd14_2 bd14_3 bd14_4 bd14_5 bd14_6 bd14_7 bd14_8, gen(b_domain14) replace normby(control)
	swindex d14_1 d14_2 d14_3 d14_4 d14_5 d14_6 d14_7 d14_8 	if wave == 2, gen(e_domain14) replace normby(control)
	swindex d14_1 d14_2 d14_3 d14_4 d14_5 d14_6 d14_7 d14_8 	if wave == 4, gen(e_domain14e) replace normby(control)
	replace e_domain14 = e_domain14e 	if wave == 4
	la var e_domain14 "\shortstack{Other Tribes:\\Contact\\Index}"

***** Domain 15: Beliefs about economic effects of Ugandans from another tribe
	swindex bd15_1, gen(b_domain15) replace normby(control)
	swindex d15_1 						if wave == 2, gen(e_domain15) replace normby(control)
	swindex d15_1 						if wave == 4, gen(e_domain15e) replace normby(control)	
	replace e_domain15 = e_domain15e 	if wave == 4
	la var e_domain15 "\shortstack{Other Tribes:\\Economic\\Beliefs\\Index}"

***** Domain 16: Social attitudes about Ugandans from another tribe
	swindex bd16_1, gen(b_domain16) replace normby(control)
	swindex d16_1 						if wave == 2, gen(e_domain16) replace normby(control)
	swindex d16_1 						if wave == 4, gen(e_domain16e) replace normby(control)	
	replace e_domain16 = e_domain16e 	if wave == 4
	la var e_domain16 "\shortstack{Other Tribes:\\Social\\Proximity\\Index}"

***** Domain 17: Gender roles
	// Generate separately for men and women per Thomas' email on Oct 9, 2022
	swindex bd17_1_female bd17_2, gen(b_domain171) replace normby(control)
	swindex d17_1_female d17_2 			if wave == 2, gen(e_domain171) replace normby(control)
	swindex d17_1_female d17_2 			if wave == 4, gen(e_domain171e) replace normby(control)
	replace e_domain171 = e_domain171e 	if wave == 4
	la var e_domain171 "\shortstack{Gender\\Role\\Index}"
	
	swindex bd17_1_male bd17_3, gen(b_domain172) replace normby(control)
	swindex d17_1_male d17_3 			if wave == 2, gen(e_domain172) replace normby(control)
	swindex d17_1_male d17_3 			if wave == 4, gen(e_domain172e) replace normby(control)
	replace e_domain172 = e_domain172e 	if wave == 4
	la var e_domain172 "\shortstack{Gender\\Role\\Index}"	

***** Domain 18: COVID-19 Household Shock
	swindex d18_1 d18_2 d18_3 d18_4 d18_5, gen(e_domain18) replace normby(control) flip(d18_5)
	la var e_domain18 "\shortstack{COVID\\Shock\\Index}"

****** Domain 19: Attribution (for code)
	swindex d19_1 d19_2 d19_3 d19_4, gen(e_domain19) replace normby(control)
	
	la var e_domain19 "\shortstack{Attribution\\Index}"

****** Domain 20: Own Tribe (for code)
	swindex bd20_1, gen(b_domain20) replace normby(control)
	swindex d20_1 						if wave == 2, gen(e_domain20) replace normby(control)
	swindex d20_1 						if wave == 4, gen(e_domain20e) replace normby(control)
	replace e_domain20 = e_domain20e 	if wave == 4
	la var e_domain20 "\shortstack{Own Tribe\\Index}"
	
****** Domain 21: Attitudes towards Child Labor 
	swindex d21_1 d21_2, gen(e_domain21) replace normby(control)
	la var e_domain21 "\shortstack{Child Labor\\Attitudes\\Index$^+$}"
	
end
Anderson


*++++ Gen business practice variables for heterogeneity analysis ++++*
// different from Domain 9a, 9b, and 9c
capture program drop BPHeterogeneity
program BPHeterogeneity

***** Marketing (different from Domain 9a, no d9a_7)
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 	if wave == 2, gen(bp_marketing) replace normby(control)
	swindex d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 	if wave == 4, gen(bp_marketinge) replace normby(control)
	replace bp_marketing = bp_marketinge 	if wave == 4
	la var bp_marketing "\shortstack{Marketing\\Index}"
	
***** Stock
	swindex d9b_1 d9b_2 d9b_3 			if wave == 2, gen(bp_stock) replace normby(control)
	swindex d9b_1 d9b_2 d9b_3 			if wave == 4, gen(bp_stocke) replace normby(control)
	replace bp_stock = bp_stocke 	if wave == 4
	la var bp_stock "\shortstack{Stock\\Index}"

***** Record keeping (different from Domain 9c, no d9c_3)
	swindex d9c_1 d9c_2 			if wave == 2, gen(bp_record) replace normby(control)
	swindex d9c_1 d9c_2 			if wave == 4, gen(bp_recorde) replace normby(control)
	replace bp_record = bp_recorde 	if wave == 4
	la var bp_record "\shortstack{Record-keeping\\Index}"
	
***** BP22 to BP25
	gen bp_bp22 = d9_7
		la var bp_bp22 "\shortstack{Marketing}" 
	gen bp_bp23 = d9_8
		la var bp_bp23 "\shortstack{Record\\Keeping}" 
	gen bp_bp24 = d9_9
		la var bp_bp24 "\shortstack{Sell\\on Credit}" 
	gen bp_bp25 = d9_10	
		la var bp_bp25 "\shortstack{Buy\\on Credit}" 
		
end
BPHeterogeneity


*++++ Gen heterogeneity analysis variable ++++*
capture program drop Heterogeneity
program Heterogeneity

preserve 

***** Mentor variables
* Baseline profits of the mentor
use "$path/Data/raw_uganda.dta", clear

	keep if (u_mentor == 1 | r_mentor == 1) // 170 Ugandan mentors, 169 refugee mentors; 168 in the U-Mentee group, 169 in the R-Mentee group
	
	keep ent_id b_b38 br_b38  u_mentor r_mentor t_ugandan
	duplicates drop // 170 Ugandan mentors, 169 refugee mentors

	gen h_mentor_profit = .
	replace h_mentor_profit = b_b38 		if t_ugandan == 1
	replace h_mentor_profit = br_b38 		if t_ugandan == 0
	qui sum h_mentor_profit, de
	replace h_mentor_profit = `r(mean)' 	if h_mentor_profit == .

	keep ent_id h_mentor_profit
	rename ent_id mentor_match_id // for matching purpose

tempfile mentor_profit
save `mentor_profit'
	
* Business practices: Endline for Ugandan mentors
use "$path/Data/raw_uganda.dta", clear

	gen wave = .
		replace wave = 1 	if _merge_midline == 3 // 1119 respondents
		replace wave = 2 	if _merge_endline == 3 // | _merge_endline_remote == 4 // 1041 respondents
		replace wave = 3	if _merge_phone2  == 3 // 1071 respondents
		replace wave = 4 	if _merge_endline2 == 3 // 901 respondents
	
	keep if u_mentor == 1 & e_bpstart != ""
	
	keep ent_id e_bp1-e_bp11 e_bp22-e_bp25 wave
	
	** Marketing
	forval i = 1/6 {
		gen h_mentor_marketing_`i' = .
	}
	
	* e_bp1: Visited at least one of your competitor's businesses to see what prices your competitors are charging.
	replace h_mentor_marketing_1 = 1 	if wave == 2 & e_bp1 == 1
	replace h_mentor_marketing_1 = 0 	if wave == 2 & (e_bp1 == 2 | e_bp1 == 3)	
	
	* e_bp2: Visited at least one of your competitor's businesses to see what products your competitors have available for sale.
	replace h_mentor_marketing_2 = 1 	if wave == 2 & e_bp2 == 1
	replace h_mentor_marketing_2 = 0 	if wave == 2 & (e_bp2 == 2 | e_bp2 == 3)
	
	* e_bp3: Asked existing customers whether there are any other products the customers would like the business to sell or produce.
	replace h_mentor_marketing_3 = 1 	if wave == 2 & e_bp3 == 1
	replace h_mentor_marketing_3 = 0 	if wave == 2 & e_bp3 == 2

	* e_bp4: Talked with at least one former customer to find out why former customers have stopped buying from your business.
	replace h_mentor_marketing_4 = 1 	if wave == 2 & e_bp4 == 1
	replace h_mentor_marketing_4 = 0 	if wave == 2 & (e_bp4 == 2 | e_bp4 == 3)

	* e_bp5: Asked a supplier about which products are selling well in your business' industry.
	replace h_mentor_marketing_5 = 1 	if wave == 2 & e_bp5 == 1
	replace h_mentor_marketing_5 = 0 	if wave == 2 & e_bp5 == 2
	
	* e_bp6: Attracted customers with a special offer. 
	replace h_mentor_marketing_6 = 1 	if wave == 2 & e_bp6 == 1
	replace h_mentor_marketing_6 = 0 	if wave == 2 & e_bp6 == 2
		
	
	** Stock control
	forval i = 1/3 {
		gen h_mentor_stock_`i' = .
	}	
	
	* e_bp7: Attempted to negotiate with a supplier for a lower price on raw material.
	replace h_mentor_stock_1 = 1 	if wave == 2 & e_bp7 == 1
	replace h_mentor_stock_1 = 0 	if wave == 2 & e_bp7 == 2

	* e_bp8: Compared the prices or quality offered by alternate suppliers or sources of raw materials to your business' current suppliers or sources of raw material.
	replace h_mentor_stock_2 = 1 	if wave == 2 & e_bp8 == 1
	replace h_mentor_stock_2 = 0 	if wave == 2 & e_bp8 == 2
	
	* e_bp9: Ran out of stock or raw materials once per month or more.
	replace h_mentor_stock_3 = 1 	if wave == 2 & e_bp9 == 2	
	replace h_mentor_stock_3 = 0 	if wave == 2 & (e_bp9 == 1 | e_bp9 == 3)
		
	
	** Record keeping
	forval i = 1/2 {
		gen h_mentor_record_`i' = .
	}		
	
	* e_bp10: Record every purchase and sale made by your business.
	replace h_mentor_record_1 = 1 	if wave == 2 & e_bp10 == 1
	replace h_mentor_record_1 = 0 	if wave == 2 & e_bp10 == 2	
	
	* e_bp11: Have a written budget, which states how much is owed each month for rent, electricity, equipment maintenance, transport, advertising, and other indirect costs to business.
	replace h_mentor_record_2 = 1 	if wave == 2 & e_bp11 == 1
	replace h_mentor_record_2 = 0 	if wave == 2 & e_bp11 == 2
		
	
	** BP22 to BP25
	gen h_mentor_bp22 = e_bp22
	gen h_mentor_bp23 = e_bp23	
	gen h_mentor_bp24 = e_bp24	
	gen h_mentor_bp25 = e_bp25	
	
	
	keep ent_id h_mentor_marketing_* h_mentor_stock_* h_mentor_record_* h_mentor_bp*
	rename ent_id mentor_match_id // for matching purpose
	
tempfile mentor_bp_ugandan
save `mentor_bp_ugandan'
	
	
* Business practices: Phone2 for refugee mentors
use "$path/Data/raw_uganda.dta", clear

	gen wave = .
		replace wave = 1 	if _merge_midline == 3 // 1119 respondents
		replace wave = 2 	if _merge_endline == 3 // | _merge_endline_remote == 4 // 1041 respondents
		replace wave = 3	if _merge_phone2  == 3 // 1071 respondents
		replace wave = 4 	if _merge_endline2 == 3 // 901 respondents
	
	keep if r_mentor == 1 & p2_bpstart != ""
	
	keep ent_id p2_bp1-p2_bp11 p2_bp22-p2_bp25 wave
	
	** Marketing
	forval i = 1/6 {
		gen h_mentor_marketing_`i' = .
	}
	
	* p2_bp1: Visited at least one of your competitor's businesses to see what prices your competitors are charging.
	replace h_mentor_marketing_1 = 1 	if wave == 3 & p2_bp1 == 1
	replace h_mentor_marketing_1 = 0 	if wave == 3 & (p2_bp1 == 2 | p2_bp1 == 3)	
	
	* p2_bp2: Visited at least one of your competitor's businesses to see what products your competitors have available for sale.
	replace h_mentor_marketing_2 = 1 	if wave == 3 & p2_bp2 == 1
	replace h_mentor_marketing_2 = 0 	if wave == 3 & (p2_bp2 == 2 | p2_bp2 == 3)
	
	* p2_bp3: Asked existing customers whether there are any other products the customers would like the business to sell or produce.
	replace h_mentor_marketing_3 = 1 	if wave == 3 & p2_bp3 == 1
	replace h_mentor_marketing_3 = 0 	if wave == 3 & p2_bp3 == 2

	* p2_bp4: Talked with at least one former customer to find out why former customers have stopped buying from your business.
	replace h_mentor_marketing_4 = 1 	if wave == 3 & p2_bp4 == 1
	replace h_mentor_marketing_4 = 0 	if wave == 3 & (p2_bp4 == 2 | p2_bp4 == 3)

	* p2_bp5: Asked a supplier about which products are selling well in your business' industry.
	replace h_mentor_marketing_5 = 1 	if wave == 3 & p2_bp5 == 1
	replace h_mentor_marketing_5 = 0 	if wave == 3 & p2_bp5 == 2
	
	* p2_bp6: Attracted customers with a special offer. 
	replace h_mentor_marketing_6 = 1 	if wave == 3 & p2_bp6 == 1
	replace h_mentor_marketing_6 = 0 	if wave == 3 & p2_bp6 == 2
		
	
	** Stock control
	forval i = 1/3 {
		gen h_mentor_stock_`i' = .
	}	
	
	* p2_bp7: Attempted to negotiate with a supplier for a lower price on raw material.
	replace h_mentor_stock_1 = 1 	if wave == 3 & p2_bp7 == 1
	replace h_mentor_stock_1 = 0 	if wave == 3 & p2_bp7 == 2

	* p2_bp8: Compared the prices or quality offered by alternate suppliers or sources of raw materials to your business' current suppliers or sources of raw material.
	replace h_mentor_stock_2 = 1 	if wave == 3 & p2_bp8 == 1
	replace h_mentor_stock_2 = 0 	if wave == 3 & p2_bp8 == 2
	
	* p2_bp9: Ran out of stock or raw materials once per month or more.
	replace h_mentor_stock_3 = 1 	if wave == 3 & p2_bp9 == 2	
	replace h_mentor_stock_3 = 0 	if wave == 3 & (p2_bp9 == 1 | p2_bp9 == 3)
		
	
	** Record keeping
	forval i = 1/2 {
		gen h_mentor_record_`i' = .
	}		
	
	* p2_bp10: Record every purchase and sale made by your business.
	replace h_mentor_record_1 = 1 	if wave == 3 & p2_bp10 == 1
	replace h_mentor_record_1 = 0 	if wave == 3 & p2_bp10 == 2	
	
	* p2_bp11: Have a written budget, which states how much is owed each month for rent, electricity, equipment maintenance, transport, advertising, and other indirect costs to business.
	replace h_mentor_record_2 = 1 	if wave == 3 & p2_bp11 == 1
	replace h_mentor_record_2 = 0 	if wave == 3 & p2_bp11 == 2
	
	
	** BP22 to BP25
	gen h_mentor_bp22 = p2_bp22
	gen h_mentor_bp23 = p2_bp23	
	gen h_mentor_bp24 = p2_bp24	
	gen h_mentor_bp25 = p2_bp25	
	
	
	keep ent_id h_mentor_marketing_* h_mentor_stock_* h_mentor_record_* h_mentor_bp*
	rename ent_id mentor_match_id // for matching purpose
	
tempfile mentor_bp_refugee
save `mentor_bp_refugee'
	

* Business practice: combine Ugandan mentors and refugee mentors
use `mentor_bp_ugandan'
	append using `mentor_bp_refugee'
	
	swindex h_mentor_marketing_1 h_mentor_marketing_2 h_mentor_marketing_3 h_mentor_marketing_4 h_mentor_marketing_5 h_mentor_marketing_6, gen(h_mentor_marketing) replace	
	swindex h_mentor_stock_1 h_mentor_stock_2 h_mentor_stock_3, gen(h_mentor_stock) replace		
	swindex h_mentor_record_1 h_mentor_record_2, gen(h_mentor_record) replace	
	
	drop h_mentor_marketing_* h_mentor_stock_* h_mentor_record_*
	
tempfile mentor_bp
save `mentor_bp'
	

* Merge all mentor variables: business profit and business practices
use `mentor_profit'

	merge 1:1 mentor_match_id using `mentor_bp'
		drop _merge
		
	* Assign value to missing values
	foreach var of varlist h_mentor_marketing h_mentor_stock h_mentor_record h_mentor_bp* {
		qui sum `var', de
		replace `var' = `r(mean)' 	if `var' == .	
	}

sleep 5000
save "$path/Data/mentor.dta", replace

restore


***** Mentee variables

** Generate variables used to construct baseline index
* Gender
	gen h_female = ls_b4 == 2 	if ls_b4 != . 

* Baseline business profit (Domain 2)
	gen h_profit = b_domain2

* Baseline support for inclusive hosting (Domain 1)
	gen h_domain1 = b_domain1

* Baseline beliefs about economic effects of refugees (Domain 4)
	gen h_domain4 = b_domain4

* Baseline social attitudes about refugees (Domain 6)
	gen h_domain6 = b_domain6

* Baseline contact with refugees by choice (Domain 7)
	gen h_domain7 = b_domain7

* Baseline contact with refugees by circumstance (Domain 8)
	gen h_domain8 = b_domain8
	
* Baseline household well-being (Domain 10)
	gen h_domain10 = b_domain10

* Baseline knowledge of whether international donations to refugees are shared with Ugandans (Domain 3, b_f15)
	gen h_domain3 = 0
	replace h_domain3 = 1 	if b_f15 == 1 // "Yes" is correct

* Baseline business networks size (BN module)
	gen h_network = b_bn_repeat_count

* Baseline business practices (Domain 9)
	gen h_domain9 = b_domain9

* Baseline sympathy
	gen h_sympathy = (bd6_3 >= 0.5)

* Match w/ mentor
	merge m:1 mentor_match_id using "$path/Data/mentor.dta"
	drop if _merge == 2
	drop _merge
	sleep 500
	erase "$path/Data/mentor.dta"

	gen h_mentor_exp = mentor_exp
	gen h_mentor_dist = mentor_dist
	

* Make heterogeneity variable binary
foreach var in h_profit h_domain1 h_domain4 h_domain6 h_domain7 h_domain8 h_domain9 h_domain10 h_network h_mentor_profit h_mentor_marketing h_mentor_stock h_mentor_record h_mentor_exp h_mentor_dist {
	qui sum `var', de
	replace `var' = `var' >= `r(p50)' 	if `var' != .
	replace `var' = 0 					if `var' == . // for non-mentor group
	
}

foreach var of varlist h_mentor_bp* {
		qui count if `var' > 3 & `var' != .
		local num_high = r(N)
		qui count if `var' < 3
		local num_low = r(N)
			replace `var' = (`var' <= 3)	if `var' != . & `num_low' < `num_high'
			replace `var' = (`var' < 3) 	if `var' != . & `num_low' >= `num_high'		 //in case agree and disagree are the same size, put neutral in disagree
	replace `var' = 0 					if `var' == . // for non-mentor group	
}

***** Public service use variables (e2_d21 & e2_e20; Endline2 only)

* e2_d21: In the last 6 months, about how many times has someone in your household visited health clinics run by the Government of Uganda? 
	gen h_d21 = e2_d21
	qui sum h_d21, de
*	replace h_d21 = `r(p50)' 	if h_d21 == . & wave == 4
	egen h_d21_std = std(h_d21)
	
* e2_e20: Do your children go to school with any children from another country? If so, which countries are they from?
	gen h_e20 = .
		replace h_e20 = 1 	if e2_e20 != "" & e2_e20 != "1" & wave == 4
		replace h_e20 = 0 	if (e2_e20 == "1" | e2_e20 == "") & wave == 4 
end
Heterogeneity


*++++ Gen attrition analysis variable ++++*
capture program drop Attrition
program Attrition

* Flag for being surveyed
gen finish_flag = .
	replace finish_flag = 1 	if wave_midline == 1 | wave_endline == 1 | wave_phone2 == 1 | wave_endline2 == 1
	replace finish_flag = 0 	if _merge_midline == 1 | (_merge_endline == 1 & _merge_endline_remote == 1) | _merge_phone2 == 1 | _merge_endline2 == 1
	la var finish_flag "Surveyed"
	
	
end
Attrition


*++++ Gen Priming variable ++++*
capture program drop Priming
program Priming

	egen strata_prime = group(treatment high_support)
	destring e_prime_treat, replace
	la var e_prime_treat "Primed On Aid Received"

	swindex bd5a_1 bd5a_2 bd5b_1 bd6_3 bd4_4, generate(bprime_index) normby(control) replace
	gen missing_bprime_index = 1 
	swindex d5a_1 d5a_2 d5b_1 d6_3 d4_4 	if wave == 2, generate(prime_index) normby(control) replace 
	swindex d5a_1 d5a_2 d5b_1 d6_3 d4_4 	if wave == 4, generate(prime_indexe) normby(control) replace 
	replace prime_index = prime_indexe 		if wave == 4
	la var prime_index "\shortstack{Primed\\Outcomes\\Index}"

end
Priming




**** Balance across refugee/ugandan mentors

*==== Gen Sum Stat Variables ====*
* Age
gen age = ls_b3a
	replace age = 21.5 	if ls_b3b == 1
	replace age = 28 		if ls_b3b == 2
	replace age = 33	 	if ls_b3b == 3
	replace age = 38	 	if ls_b3b == 4
	replace age = 43	 	if ls_b3b == 5
	replace age = 48	 	if ls_b3b == 6
	replace age = 58 		if ls_b3b == 8
	replace age = b_lic5 	if b_lic4 == 2
	replace age = br_lic5 	if br_lic4 == 2

la var age "Age (Years)"

* Education
gen edu = b_p12 - 1 if b_p12 <= 14
	replace edu = 15 	if b_p12 == 15 // Some college
	replace edu = 17 	if b_p12 == 16 // College degree
	replace edu = 18 	if b_p12 == 17 // Some graduate school
	replace edu = 19 	if b_p12 == 18 // Graduate degree

replace edu = br_p14 - 1 if br_p14 <= 14
	replace edu = 15 	if br_p14 == 15 // Some college
	replace edu = 17 	if br_p14 == 16 // College degree
	replace edu = 18 	if br_p14 == 17 // Some graduate school
	replace edu = 19 	if br_p14 == 18 // Graduate degree	
	
la var edu "Education (Years)"

* % Women
gen female = .
replace female = 1 	if ls_b4 == 2
replace female = 0 	if ls_b4 == 1

la var female "Female"

* % Tailor
gen tailor = .
replace tailor = 1 	if ls_sector_name == "Tailor"
replace tailor = 0 	if ls_sector_name == "Salon"

la var tailor "Tailor"

* Experience
gen experience = ls_sector_experience

la var experience "Experience in Sector (Years)"

* Profit
gen profit = tcon_b_b38
	replace profit = profit / 3695
la var profit "Profit (USD/Month)"

* Any employees
gen employee = .
replace employee = 1 	if b_b31 > 0 & b_b31 != . 
replace employee = 0 	if b_b31 == 0 
replace employee = 1 	if br_b31 > 0 & br_b31 != . 
replace employee = 0 	if br_b31 == 0 

la var employee "Has Any Employees"

sort ent_id wave_flag 

